*program is in c* Write a program to find the words that have a specific charact
ID: 3831664 • Letter: #
Question
*program is in c*
Write a program to find the words that have a specific character among the words of a string. Read a string and a character from the user and display all the words having that character on the screen. One way to do this is to find the words in one sentence and check if they have the required character in them. Another way is to find all the occurrence of the character and then find the words in those locations. Assume that words are not case sensitive in your program. Both the words "One" and "one" have an 'o' in them. Do not use string function strtok(). You can write your own version of strtok() and use them in your program if you want. Sample execution is given below Enter a sentence This is my first sentence Enter a character i Words containing i are: This is first Since words are case sensitive we have the following sample execution Enter a sentence I am a computer scientist Enter a character i Words containing i are: I scientistExplanation / Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int isCharinWord(char *s, char c)
{
int length = strlen(s);
int i = 0;
for(i = 0; i < length; i++)
{
if(tolower(s[i]) == tolower(c))
{
return 1;
}
}
return 0;
}
int main()
{
char *sentence;
size_t len = 1000;
sentence = (char *)malloc(sizeof(char)*len);
printf("Enter a sentence ");
getline(&sentence, &len, stdin);
int length = strlen(sentence);
printf("Enter a character ");
char c;
scanf("%c", &c);
int i = 0;
char word[100];
int j = 0;
for(i = 0; i < length; i++)
{
if(sentence[i] != ' ' && sentence[i] != ' ')
{
word[j++] = sentence[i];
}
else
{
word[j] = '';
if(isCharinWord(word, c))
{
printf("%s ", word);
}
j = 0;
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.