C strupr() and strstr() function & use

The strupr() and strstr() are two very useful string functions in C where strupr() string function is used to convert a string to uppercase and strstr() function is used to take a substring from the main string depend on the matched content from where we want to take the substring till the last character of the main string. In this article we will see some C program to learn strupr() and strstr() function. So, lets get started.

strupr() function in C, uppercase string

strupr() function converts whole string to uppercase and return string characters in uppercase. We use strupr() to convert all the character in uppercase. See the C program bellow.

// strupr() function in C, uppercase string
#include<stdio.h>
#include <string.h>
int main(){
  char myString[20];
  printf("Enter a string to convert : ");
  
  gets(myString);
  
  printf("\nsYou have entered string : %s",myString);
  printf("\nString in uppercase is : %s\n",strupr(myString));
 return 0;
}

Output of this uppercase string program:

strupr() function in C, uppercase string

strstr() function in C

strstr() function returns another string which is sub string of the main string. It returns the pointer to the first occurrence of matched string till the last character.

strstr() takes two parameters to perform its function. One is the main string where it needs to search and another is the matched content. See the syntax of strstr() function here.

// syntax of strstr() function

char *strstr(const char *string, const char *matched);

Consider the following program where we have used the strstr() function to print a sub string from the main string.

// strstr() function in C
#include<stdio.h>
#include <string.h>
int main(){
  char mainString[100]="I love C programming with competitive programmer";
  printf("Main string is : \n%s\n", mainString);

  char *subString;

  subString = strstr(mainString,"with");

  printf("\nSubstring is : \n%s\n",subString);

 return 0;
}

Output of strstr() function program

strstr() function in C, making a substring

If you have still any doubt about these strupr() and strstr() string functions, then don’t forget to discuss on our discussion section.