Programming with C

⌘K
  1. Home
  2. Docs
  3. Programming with C
  4. Functions and Pointers
  5. Built-in functions
  6. String Substring

String Substring

Use of strstr( ) function

#include <stdio.h>
#include<string.h>
int main(){
	char s1[100];
	char s2[100];
	char *ptr;
	puts("Enter string1");
	gets(s1);
	puts("Enter string2");
	gets(s2);
	ptr = strstr(s1,s2);
	if(ptr == NULL){
	    puts("string not present");
	}
	else{
	    printf("string present at %d",ptr-s1);
	}
	return 0;
}

Output

Enter string1
Hello welcome
Enter string2
wel
string present at 6

Use of strstr( ) function

#include<stdio.h>
#include<string.h>
int main() 
{
    char s[10] ="123456789";
   char ch = 'Z';
    puts("string before setting");
    puts(s);
   strset(s,ch);
   puts("string after setting");
    puts(s);
   return 0;
}

Output

string before setting
123456789
string after setting
ZZZZZZZZZ

Views: 0

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments