![]() |
| | #16 |
| Registered User Join Date: Nov 2008
Posts: 31
| 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]);
}
}
}
Thanks again tabstop. |
| w2look is offline | |
| | #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 | |
| | #18 | |
| Registered User Join Date: Nov 2008
Posts: 31
| Quote:
Any suggestions on a fix? | |
| w2look is offline | |
| | #19 |
| Registered User Join Date: Oct 2008 Location: TX
Posts: 1,262
| Can you show what the code for the next part of the problem looks like? |
| itCbitC is offline | |
| | #20 |
| Registered User 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++;
}
|
| w2look is offline | |
| | #21 | |
| Registered User Join Date: Nov 2008
Posts: 31
| Quote:
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 | |
![]() |
| Tags |
| delimiters, function, parse, parsing, strings |
| Thread Tools | |
| Display Modes | |
|
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 |