0% found this document useful (0 votes)
52 views2 pages

New Pro STRNCMP & strstr11

Uploaded by

Chetan Cherry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views2 pages

New Pro STRNCMP & strstr11

Uploaded by

Chetan Cherry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Implement my_strcmp( ) function

#include <stdio.h>
#include <string.h>
int my_strcmp(char *s1, char *s2, int n);
int main() {
char s1[20], s2[20];
int r, n;
printf("Enter s1 string: ");
scanf("%s", s1);
printf("Enter s2 string: ");
scanf("%s", s2);
printf("Enter number: ");
scanf("%d", &n);
r = my_strcmp(s1, s2, n);
if (r == 0)
printf("Both strings are the same");
else if (r > 0)
printf("String 1 is greater than string 2 ");
else
printf("String 2 is greater than string 1 ");
return 0;
}
int my_strcmp(char *s1, char *s2, int n) {
while (n > 0 && *s1 == *s2)
{
s1++;
s2++;
n--;
}
if(n==0)
return 0;
else
return (*s1 - *s2);
}==============***==============
Implement my_strstr( ) function
#include <stdio.h>
char *my_strstr(char *s1, char *s2);
int main()
{
char s1[30], s2[20], *p;
printf("Enter string1:\n");
scanf("%[^\n]s", s1);
printf("Enter string2:\n");
scanf("%s", s2);
printf("str1 address %p\n", s1);
p = my_strstr(s1, s2);
if (p == NULL)
printf("str2 is not present in str1\n");
else
printf("str2 is present in str1 at %p\n", p);
return 0;
}
char *my_strstr(char *s1, char *s2) {
char *t, *p1, *p2;
p1 = s1;
p2 = s2;
while (*p1)
{
t = p1;
while (*p1 == *p2 && *p2 != '\0')
{
p1++;
p2++;
}
if (*p2 == '\0')
{
return t;
}
p1=t+1;
p2 = s2;
}
return NULL;
}

You might also like