Help with parsing a sentence.

This is a discussion on Help with parsing a sentence. within the C Programming forums, part of the General Programming Boards category; OK, this time I think I've got it. Code: void ParseSentence(char *ptrA) { char temp[80]; int i; strcpy(temp, ptrA); int ...

  1. #16
    Registered User w2look's Avatar
    Join Date
    Nov 2008
    Posts
    31
    OK, this time I think I've got it.

    Code:
    void ParseSentence(char *ptrA)
    {
    	char temp[80];
    	int i;
    	
    	strcpy(temp, ptrA);
    	
    	int size = strlen(temp);
    	char *words[80];
    
    	words[0] = strtok(temp, " .,;");
    	
    	printf("%s\n", words[0]);
    	
    	for(i = 1; i < size - 1; i++)
    	{
    		while(words[i] != NULL)
    		{
    			words[i] = strtok(NULL, " .,;");
    			
    			if(words[i] == NULL)
    			{
    				break;
    			}
    			
    			printf("%s\n", words[i]);
    		}
    	}
    }
    Seems to work good. Tried it with multiple input, varying punctuation.

    Thanks again tabstop.

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, no!

    You now have a loop inside another loop - and it checks a variable that hasn't been set yet (words[i] is not defined).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    Registered User w2look's Avatar
    Join Date
    Nov 2008
    Posts
    31
    Quote Originally Posted by matsp View Post
    Ehm, no!

    You now have a loop inside another loop - and it checks a variable that hasn't been set yet (words[i] is not defined).

    --
    Mats
    I see what you're saying, I have since moved on to the next part of this problem and it is causing me problems. While I am getting the printed output I desired, the words are not being stored in the indexes of the array words[].

    Any suggestions on a fix?

  4. #19
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by w2look View Post
    I see what you're saying, I have since moved on to the next part of this problem and it is causing me problems. While I am getting the printed output I desired, the words are not being stored in the indexes of the array words[].

    Any suggestions on a fix?
    Can you show what the code for the next part of the problem looks like?

  5. #20
    Registered User w2look's Avatar
    Join Date
    Nov 2008
    Posts
    31
    Actually, I think I got it!

    Changed the code in the for loop to:

    Code:
    	for(i = 1; i < size - 1; i++)
    	{
    
    			words[i] = strtok(NULL, " .,;");
    			
    			if(words[i] == NULL)
    			{
    				break;
    			}
    			count++;
    	}
    Now, I believe it works correctly. Thanks for clarifying my mistake Mats!

  6. #21
    Registered User w2look's Avatar
    Join Date
    Nov 2008
    Posts
    31
    Quote Originally Posted by itCbitC View Post
    Can you show what the code for the next part of the problem looks like?
    Why not?

    The next part of the problem involved taking the newly "parsed" array of words, sorting them in alphabetical order and then printing them.

    Code:
    //prototypes
    void ParseSentence(char *ptrA);
    void BubbleSort(char *ptrA[], const int size);
    void PrintList(char *ptrA[], const int size);
    
    //call in main
    ParseSentence(Buffer3);
    
    //Break a sentence up into words, sort them alphabetically and print them
    //Input: a NULL terminated string
    //Output:a printed list of the words in the string sorted alphabetically
    void ParseSentence(char *ptrA)
    {
    	char temp[80];
    	int i, j;
    	int count = 1;
    	
    	strcpy(temp, ptrA);
    	
    	int size = strlen(temp);
    	char *words[80];
    
    	words[0] = strtok(temp, " .,;");
    	
    	printf("%s\n", words[0]);
    	
    	for(i = 1; i < size - 1; i++)
    	{
    
    			words[i] = strtok(NULL, " .,;");
    			
    			if(words[i] == NULL)
    			{
    				break;
    			}
    			count++;
    	}
    
    	printf("Buffer3 in alphabetical order(Capital letters have priority):\n\n");
    	BubbleSort(words, count);
    	PrintList(words, count);
    }
    
    //Now that we have an array of words, we need to sort them alphabetically
    //Input: an array or words and it's size
    //Output: the words in the array sorted alphabetically
    void BubbleSort(char *ptrA[], const int size)
    {
       int pass;
       int j;
       char *tempPtr;
    
       for (pass=0; pass < size - 1; pass++)
       {
          for (j=0; j<size - 1; j++)
          {
             if (strcmp(ptrA[j], ptrA[j + 1]) > 0)
             {
                /* swap pointers */
                tempPtr = ptrA[j];
                ptrA[j] = ptrA[j+1];
                ptrA[j+1] = tempPtr;
             }
          }
       }
    }
    
    //Once the list of words is sorted, we need to print it
    //Input: the array of words to be printed and it's size
    //Output:
    void PrintList(char *ptrA[], const int size)
    {
       int i;
       for (i = 0; i < size; i++)
       {
          printf("%s\n", ptrA[i]);
       }
    }

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector<...>::iterators and insert - I'm stumped.
    By Dino in forum C++ Programming
    Replies: 6
    Last Post: 12-25-2007, 06:11 AM
  2. draw tree graph of yacc parsing
    By talz13 in forum C Programming
    Replies: 2
    Last Post: 07-23-2006, 02:33 AM
  3. mafia game
    By italiano40 in forum Game Programming
    Replies: 7
    Last Post: 11-07-2005, 01:22 AM
  4. Parsing for Dummies
    By MisterWonderful in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2004, 05:31 PM
  5. I hate string parsing with a passion
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-19-2002, 07:30 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21