Write a function called ParseSentence that takes as an input parameter a null-te
ID: 3727796 • Letter: W
Question
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.
PLEASE DOCUMENT THE CODE WITH COMMENTS
Explanation / Answer
#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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.