1

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

Cherusker
  • 1,576
  • 1
  • 9
  • 18
DAVID
  • 21
  • 2
  • 1
    You need to store the start locations of the target word, like 1 and 22. And you know the length of the target word, too. So when you post-process your input string, use `toupper` within that region only (1-4 and 22-25). – Soumya Kanti Jan 19 '19 at 15:54
  • Please properly format your code: proper indentation and remove unnecessary empty lines. – Paul Ogilvie Jan 19 '19 at 16:06
  • 3
    Possible duplicate of [What is the function to replace string in C?](https://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c) – Risch Jan 19 '19 at 16:28
  • DAVID, If looking for "aba", should "xyx_ababa" print as "xyx_ABAba" or "xyx_ABABA"? – chux - Reinstate Monica Jan 19 '19 at 17:59
  • 1
    "i use the the functions toupper from the C library ctype.h, the entire text is converted into uppercase letters" --> post that use. – chux - Reinstate Monica Jan 19 '19 at 18:03

2 Answers2

1

Your code might crash if the there is no input.

Use the standard function strstr(). Why to reinvent the wheel? You can also improve the complexity of the code. See this link:

https://www.geeksforgeeks.org/frequency-substring-string/

Syed Waris
  • 1,056
  • 1
  • 11
  • 16
-1

I don't have permission to comment yet so I have to answer you here.

A char variable is encoded with 8 bits, so it can be 256 values [0,255]. Printable characters can be found in the ASCII table where you can see that the character a is equal to the decimal value 97.

If you consider characters as decimal value, I am sure you can figure out by yourself how to pass from a (97) to A (65), so you will be able to perform the same operation for any character.

Qzaac
  • 159
  • 1
  • 8