includestdioh includestringh main char str110INDIA str210INDIA char str310NITK

Includestdioh includestringh main char str110india

This preview shows page 42 - 54 out of 63 pages.

#include<stdio.h> #include<string.h> main() { char str1[10]=“INDIA", str2[10]=“INDIA"; char str3[10]="NITK", str4[10]="MITK"; //ASCII value M=77 and N=78 char str5[10]="MITK", str6[10]="NITK"; printf("result=%d\n",strcmp(str1,str2)); printf("result=%d\n",strcmp(str3,str4)); printf("result=%d",strcmp(str5,str6)); } Output : Result=0 Result=1 Result=-1
Image of page 42
Example : strcmp() #include <stdio.h> #include <string.h> int main() { char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; int result; // comparing strings str1 and str2 result = strcmp(str1, str2); printf("strcmp(str1, str2) = %d\n", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %d\n", result); result = strcmp(str2, str3); printf("strcmp(str1, str3) = %d\n", result); } Output strcmp(str1, str2) = 32 strcmp(str1, str3) = 0 strcmp(str1, str3) = -32
Image of page 43
strstr( ): This function is used to find occurrence of sub-string in main string. This function points to the first character of s2 in s1. Otherwise, a null pointer if s2 is not present in s1 . If s2 points to an empty string, s1 is returned. Syntax: strstr(first_string, second_string);
Image of page 44
Example: Program to illustrate the use of strstr(). #include <stdio.h> #include<string.h> main() { char s1[] = “Hello, how are you?” ; char s2[] = “are”; if (strstr(s1,s2)!=NULL) printf("String found\n"); else printf("String not found\n"); }
Image of page 45
Example : strstr() #include <stdio.h> #include <string.h> int main () { char str1[20] = "hello world hai"; char sub[10] = "world"; printf("The substring is: %s\n", strstr(str1, sub)); }
Image of page 46
strncpy(): This function copies the left most n characters of the source string to the destination string. Syntax: Example: strncpy(s1,s2,5); //copies first 5 characters of s2 into s1 strncpy(Destination_String, Source_String, no_of_characters );
Image of page 47
Example : strncpy() #include <stdio.h> #include <string.h> int main () { char src[40]; char dest[12]; memset(dest, '\0', sizeof(dest)); strcpy(src, "This is tutorialspoint.com"); strncpy(dest, src, 10); printf("Final copied string : %s\n", dest); }
Image of page 48
strncmp () : (practice programs) This function compares the first n characters of two strings Syntax: Example: strncmp(s1,s2,4); This compares the leftmost n characters of s1 to s2 and returns: a) 0 if they are equal b) Negative number, if s1 sub-string is less than s2 c) Otherwise, Positive number strncmp(string1, string2, no_of_chars);
Image of page 49
Example : strncmp() #include <stdio.h> #include <string.h> int main () { char str1[15]; char str2[15]; int ret; strcpy(str1, "abcdef"); strcpy(str2, "ABCDEF"); ret = strncmp(str1, str2, 4); printf("ret = %d\n",ret); if(ret < 0) printf("str1 is less than str2"); else if(ret > 0) printf("str2 is less than str1"); else printf("str1 is equal to str2"); }
Image of page 50
strncat(): This function concatenate the left most n characters of the source string to the destination string. syntax: Example: strncat(string1, string2,no_of_chars);
Image of page 51
Example : strncat() #include <stdio.h> #include <string.h> int main () { char src[50], dest[50]; strcpy(src, "This is source"); strcpy(dest, "This is destination"); strncat(dest, src, 10); printf("Final destination string : |%s|", dest); }
Image of page 52
What is the output of the following’s? Example 1: char s1[4]={‘n’, ’I’, ’t’, ’k’}; char s2[4]={‘n’, ’I’, ’t’, ’k’}; printf (“%s”, strcmp(s1,s2)); Example2: char str1[5]=“nitk1”, str2[4]=“ nitk ”; printf("compare=%d",strcmp(str1,str2)); Example 3: char str1[5]=“nitk1”, str2[4]=“ nitk ”; printf("copy=%s", strcpy(str1,str2)); Example 4: scanf (“%[^a - z]”,str1);
Image of page 53
Image of page 54

You've reached the end of your free preview.

Want to read all 63 pages?

  • Summer '18
  • ASCII, scanf

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture