Thread: Counting

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Counting

    Code:
    include<stdio.h>
    #include<string.h>
    //#include<alloc.h>
    #include<conio.h>
    
    void swap(char*,int);
    void gotoloop(char*,int);
    void main()
    {
         char *ch;
         int i,j,k,l;
         ch=(char*)malloc(20);
         //clrscr();
         printf("Enter the string:");
         gets(ch);
         
         l = strlen(ch);
         gotoloop(ch,l);
        
         
         
         
         getch();
         return;
         
    }
    void gotoloop(char *ch, int l)
    {
         int i,k;
         k = l;
         
         if(l<=1)
         return;
         
         
         for(i=0;i<k;i++)
         {
         swap(ch,k);
         l--;
         gotoloop(ch,l);
         l++;
         if(k==2)
         printf("\n%s ",ch);
        
         }
    }
    void swap(char *ch, int r)
    {
         char c;
         int i;
         
         c = ch[r-1];
         for(i=r-1;i>0;i--)
         ch[i]=ch[i-1];
         ch[0] = c;
    }
    This code produces all combinations of a word. Now how do i write code to give the sum of all the combinations produced ? Need help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you need to fix a whole bunch of things.
    SourceForge.net: cpwiki

    Then perhaps make swap() count how many times it is called?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    What changes do I need to make?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > What changes do I need to make?
    How about your entire attitude to programming?

    Permutations Of A Number Or String - C | DaniWeb
    For example, adopting the attitude that "I WILL DO ALL MY OWN HOMEWORK" and not just trawl the internet for half-assed answers on one forum, then recycle them on another forum pretending that I did the work myself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    This is not my homework! I am just trying to learn.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BST Counting
    By m0ntana in forum C Programming
    Replies: 6
    Last Post: 05-11-2007, 05:37 AM
  2. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  3. counting down...
    By twomers in forum C++ Programming
    Replies: 5
    Last Post: 12-09-2005, 02:51 PM
  4. counting
    By threahdead in forum C Programming
    Replies: 0
    Last Post: 10-19-2002, 10:33 AM
  5. Counting
    By simhap in forum C++ Programming
    Replies: 5
    Last Post: 10-08-2001, 04:06 PM