Thread: Words in a string

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    28

    Words in a string

    Hello i am looking for a little help

    i need to make a program in c to get all words from a string and save them in other strings.
    for eg. if char a[]="mary had a little lamb" i need to store all five words in different strings and later print them. and no use of string .h.
    i wrote a code but it wont give correct output any guidance will be helpful.
    Thank you

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Post your properly indented code in code tag.
    What's the exact problem with your code, + all other necessary info...

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Code:
    int splitonspace(const char *s,char ***a)
    {
      int r=0;
      while( *s )
      {
        if( *s==' ')
          ++s;
        else
        {
          size_t len=0;
          *a=realloc(*a,++r*sizeof*a);
          (*a)[r-1]=calloc(1,1);
          while( *s && *s!=' ')
          {
            (*a)[r-1]=realloc((*a)[r-1],2+len);
            (*a)[r-1][len+1] = 0;
            (*a)[r-1][len++] = *s++;
          }
        }
      }
      return r;
    }
    
    int main()
    {
      char a[]="mary had a little lamb";
      char **sa=0;
      int r=splitonspace(a,&sa);
      while( r-- )
        puts(sa[r]);
      return 0;
    }

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by BillyTKid View Post
    Code:
    int splitonspace(const char *s,char ***a)
    {
      int r=0;
      while( *s )
      {
        if( *s==' ')
          ++s;
        else
        {
          size_t len=0;
          *a=realloc(*a,++r*sizeof*a);
          (*a)[r-1]=calloc(1,1);
          while( *s && *s!=' ')
          {
            (*a)[r-1]=realloc((*a)[r-1],2+len);
            (*a)[r-1][len+1] = 0;
            (*a)[r-1][len++] = *s++;
          }
        }
      }
      return r;
    }
    
    int main()
    {
      char a[]="mary had a little lamb";
      char **sa=0;
      int r=splitonspace(a,&sa);
      while( r-- )
        puts(sa[r]);
      return 0;
    }
    You are either not very bright or just don't care about the rules of this forum to give away an answer to a potential assignment like that. I haven't checked the code and I sincerely hope it crashes.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you use strtok() to split the string, it'd be easier.
    Edit: oops, no use of string.h? You could still use memcpy to copy your string
    Last edited by Bayint Naung; 10-23-2010 at 04:09 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    BillyTKid's program is *brilliant*. He's a bit quick on the "draw", perhaps. Billy, where were you when we needed you to break the Enigma code? Elegant coders can be so annoying!

    This seems like a natural for using a linked list, with each node having one word. If mrBaines doesn't know linked lists yet, maybe just use a slightly simpler 2D array of pointers, with plenty of pointers to avoid the realloc() business, and then a simple assignment of the string's base, to the pointer of the array.

    Basically, work with mrBains with a goal of making a dumbed down version of Billy's code, so mrBains can more readily understand it, and learn from it.

    You're a good programmer BillyTKid. I'd prefer the students have more interaction and show some effort, before getting a complete program however, my 2 cents.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    28
    Quote Originally Posted by Bayint Naung View Post
    Post your properly indented code in code tag.
    What's the exact problem with your code, + all other necessary info...
    Here's my little simple program ,if any one can correct it or just tell me the flaws.

    Code:
    #include<stdio.h>
    
    main()
    {
      int i=0,j=0,k=0,x=0;
      char a[]="mary had a little lamb";
      char b[15][15];
    
      while(a[i]!='\0')
      { if(a[i]==' ')
         { for(j=x;j<=i-x;j++)
    	{
    	 b[j][k]=a[j];
    
    
    	}
           k++;
           x=i;
           }
          i++;
    
      }
    int p=k;
    printf("\n\nwords are:);
    i=0;j=0;
      for(j=0;j<p;j++)
      { i=0;
       printf("\n\n");
       while(b[i][j]!=' ')
       {
        printf("%c",b[i][j]);
        i++;
        }
    
       }
    
    
    
    }
    i can get the first word but not the rest. I know there is mistake when i am putting chars in 2d array but couldn't correct it .

    Thank you billy for your code.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I think the hardest thing about this assignment is that you have to remember that the inner loop doesn't know a thing about the outer loops testing condition, so you have to repeat it for it.

    I would use two while loops, since you never know (conceptually at least) how many words there will be to separate.

    With two while loops, it looks like:
    Code:
    int main()
    {
      int i, row, col;
      char a[]="mary had a little lamb, it's fleece was white as snow";
      char b[15][15];
      i = row=col=0;
      while(a[i]!='\0')
      {  while(a[i] != ' ' ) {
           b[row][col]= a[i];
           col++;
           
         }
         b[row][col]='\0'; //add end of string marker char
    which fails for two reasons:
    1) The inner loop goes berserk when it reaches the end of the char array, and can't find a space - endless loop.

    2) i, row, and col are either not being set up for the next letter, or not being reset for the next word. That's why your code wasn't working with the next word, by the way.

    So:

    Code:
    int main()
    {
      int i, row, col;
      char a[]="mary had a little lamb, it's fleece was white as snow";
      char b[15][15];
      i = row=col=0;
      while(a[i]!='\0')
      {  while(a[i] != ' ' && a[i]!='\0') {
           b[row][col]= a[i];
           col++;
           i++;
         }
         b[row][col]='\0'; //add end of string marker char
         row++;  //get ready for a 
         col=0;  //new row
         i++;
      }

    All together:

    Code:
    #include<stdio.h>
    
    void printIt(char b[][15], int rows);
    
    int main()
    {
      int i, row, col;
      char a[]="mary had a little lamb, it's fleece was white as snow";
      char b[15][15];
      i = row=col=0;
      while(a[i]!='\0')
      {  
        while(a[i] != ' ' && a[i]!='\0') {
           b[row][col]= a[i];
           col++;
           i++;
         }
         b[row][col]='\0'; //add end of string marker char
         row++;  //get ready for a 
         col=0;  //new row
         i++;
      }
      printIt(b, row-1);
      printf("\n\n\t\t\t    press enter when ready");
      (void) getchar();
      return 0;
    }
    void printIt(char b[][15], int row) {
      int i;
      printf("\n\nwords are: \n");
      for(i=0;i<row;i++)
        printf("\n%s",b[i]);
    }

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    28

    Thumbs up

    Quote Originally Posted by Adak View Post
    I think the hardest thing about this assignment is that you have to remember that the inner loop doesn't know a thing about the outer loops testing condition, so you have to repeat it for it.

    I would use two while loops, since you never know (conceptually at least) how many words there will be to separate.

    With two while loops, it looks like:
    Code:
    int main()
    {
      int i, row, col;
      char a[]="mary had a little lamb, it's fleece was white as snow";
      char b[15][15];
      i = row=col=0;
      while(a[i]!='\0')
      {  while(a[i] != ' ' ) {
           b[row][col]= a[i];
           col++;
           
         }
         b[row][col]='\0'; //add end of string marker char
    which fails for two reasons:
    1) The inner loop goes berserk when it reaches the end of the char array, and can't find a space - endless loop.

    2) i, row, and col are either not being set up for the next letter, or not being reset for the next word. That's why your code wasn't working with the next word, by the way.

    So:

    Code:
    int main()
    {
      int i, row, col;
      char a[]="mary had a little lamb, it's fleece was white as snow";
      char b[15][15];
      i = row=col=0;
      while(a[i]!='\0')
      {  while(a[i] != ' ' && a[i]!='\0') {
           b[row][col]= a[i];
           col++;
           i++;
         }
         b[row][col]='\0'; //add end of string marker char
         row++;  //get ready for a 
         col=0;  //new row
         i++;
      }

    All together:

    Code:
    #include<stdio.h>
    
    void printIt(char b[][15], int rows);
    
    int main()
    {
      int i, row, col;
      char a[]="mary had a little lamb, it's fleece was white as snow";
      char b[15][15];
      i = row=col=0;
      while(a[i]!='\0')
      {  
        while(a[i] != ' ' && a[i]!='\0') {
           b[row][col]= a[i];
           col++;
           i++;
         }
         b[row][col]='\0'; //add end of string marker char
         row++;  //get ready for a 
         col=0;  //new row
         i++;
      }
      printIt(b, row-1);
      printf("\n\n\t\t\t    press enter when ready");
      (void) getchar();
      return 0;
    }
    void printIt(char b[][15], int row) {
      int i;
      printf("\n\nwords are: \n");
      for(i=0;i<row;i++)
        printf("\n%s",b[i]);
    }

    Works good
    thnx for ur guidance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Converting a string of words into correct cases
    By BigFish21 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 12:53 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

Tags for this Thread