C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-22-2009, 04:34 PM   #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.
w2look is offline   Reply With Quote
Old 02-22-2009, 05:19 PM   #17
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 02-22-2009, 06:45 PM   #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?
w2look is offline   Reply With Quote
Old 02-22-2009, 06:57 PM   #19
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
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?
itCbitC is offline   Reply With Quote
Old 02-22-2009, 07:05 PM   #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!
w2look is offline   Reply With Quote
Old 02-22-2009, 07:09 PM   #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]);
   }
}
w2look is offline   Reply With Quote
Reply

Tags
delimiters, function, parse, parsing, strings

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:54 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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