String Tokenization IN C language Write a function called ParseSentence that tak
ID: 3727293 • Letter: S
Question
String Tokenization IN C language
Write a function called ParseSentence that takes as an input parameter a null-terminated string S. Where S would contain an English sentence. Assume that the delimiters are space, comma, semicolon and period. The function extracts each word from the sentence (without any punctuation or spacings) and then re-prints the words in alphabetical order. The function returns a void.
Example: ParseSentence("hello world, how are you today."); would print the following:
are
hello
how
today
world
you
Write a main method to test the function ParseSentence. You may use any built-in function from the string.h and ctype.h if required.
Explanation / Answer
solution code:
#include <stdio.h>
#include <string.h>
#define SIZE 100
void main()
{
ParseSentence("hello world, how are you today.");
}
void ParseSentence(char str[SIZE])
{
int count = 0, c = 0, i, j = 0, k, l, space = 0;
char p[50][100], str1[20], ptr1[50][100], cmp[50];
for (i = 0;i < strlen(str);i++)
{
if ((str[i] == ' ')||(str[i] == ',')||(str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46)) //checking the chracter comma and dot(, .)
{
p[i][k] = '';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
for (i = 0;i < space;i++) //loop for sorting
{
for (j = i + 1;j <= space;j++)
{
if ((strcmp(p[i], p[j]) > 0)) //copy the character string to combine a sentence in ascending order
{
strcpy(cmp, p[i]);
strcpy(p[i], p[j]);
strcpy(p[j], cmp);
}
}
}
printf("After sorting string is:");
for (i = 0;i <= space;i++)
{
printf("%s ", p[i]);
}
}
sample output:
After sorting string is:
are
hello
how
today
world
you