I want to write a C program to search all occurrences of a word in given string and to write all occurrences of a word in capital letters.
Example
Input
Input string: good morning. have a good day.
Output The word 'good' was found at location 1 The word 'good' was found at location 22
GOOD morning. have a GOOD day.
I wrote the following code
#include <stdio.h>
#include <string.h>
void main()
{
char str[1000], pat[20] = "good";
int i = 0, j, mal = 0, flag = 0;
printf("Enter the string :");
gets(str);
while (str[i] != '\0')
{
if (str[i] == pat[0])
{
j = 1;
//if next character of string and pat same
while(pat[j] != '\0' && str[j + i] != '\0' && pat[j] == str[j + i])
{
j++;
flag = 1;
}
if (pat[j] == '\0')
{
mal += 1;
printf("\n The word was found at location %d.\n" , i + 1);
}
}
i++;
if (flag == 0)
{
if (str[j + i] == '\0')
printf(" The word was not found ") ;
}
}
printf("The word was found a total of %d times", mal);
}
How can I convert the word 'good' into uppercase letters?
When i use the the functions toupper from the C library ctype.h, the entire text is converted into uppercase letters. can you help me pleas?
Thanks