Declaration: char *strstr(const char *s1, const char *s2);
Return Value:
On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1).
On error (if s2 does not occur in s1), strstr returns null.
Example:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *str1 = "CuriousTab", *str2 = "ia", *ptr;
ptr = strstr(str1, str2);
printf("The substring is: %s\n", ptr);
return 0;
}
Output: The substring is: iaCURIOUSTAB
It scans a string s in the reverse direction, looking for a specific character c.
Example:
#include <string.h>
#include <stdio.h>
int main(void)
{
char text[] = "I learn through CuriousTab.com";
char *ptr, c = 'i';
ptr = strrchr(text, c);
if (ptr)
printf("The position of '%c' is: %d\n", c, ptr-text);
else
printf("The character was not found\n");
return 0;
}
Output:
The position of 'i' is: 19
char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch
#include <stdio.h>
#include <string.h>
int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';
printf("string before strnset: %s\n", string);
strnset(string, letter, 13);
printf("string after strnset: %s\n", string);
return 0;
}
Output:
string before strnset: abcdefghijklmnopqrstuvwxyz
string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz
The strcmp return an int value that is
if s1 < s2 returns a value < 0
if s1 == s2 returns 0
if s1 > s2 returns a value > 0
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
{ length++; s++; }
return (length);
}
int xstrlen(char s)
{
int length=0;
while(*s!='\0')
length++; s++;
return (length);
}
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
length++;
return (length);
}
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
s++;
return (length);
}
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
{ length++; s++; }
return (length);
}
Example:
#include<stdio.h>
int xstrlen(char *s)
{
int length=0;
while(*s!='\0')
{ length++; s++; }
return (length);
}
int main()
{
char d[] = "CuriousTab";
printf("Length = %d\n", xstrlen(d));
return 0;
}
Output: Length = 8
#include <stdio.h>
int main(void)
{
char string[80];
printf("Enter a string:");
gets(string);
printf("The string input was: %s\n", string);
return 0;
}
Output:
Enter a string: CuriousTab
The string input was: CuriousTab
#include<stdio.h> #include<string.h> int main() { static char s[] = "Hello!"; printf("%d\n", *(s+strlen(s))); return 0; }
#include<stdio.h> #include<string.h> int main() { char str1[20] = "Hello", str2[20] = " World"; printf("%s\n", strcpy(str2, strcat(str1, str2))); return 0; }
Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello World".
=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.
Hence it prints "Hello World".
#include<stdio.h> int main() { char str1[] = "Hello"; char str2[] = "Hello"; if(str1 == str2) printf("Equal\n"); else printf("Unequal\n"); return 0; }
Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters and initialized with a string "Hello".
We have use strcmp(s1,s2) function to compare strings.
Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address of both variable is not same. Hence the if condition is failed.
Step 4: At the else part it prints "Unequal".
Copyright ©CuriousTab. All rights reserved.