0% found this document useful (0 votes)
38 views

Strings Comparison Programs

The document provides examples of using string handling functions like strcmp(), strcat(), strcpy(), strlen(), strncpy(), strncmp(), strncat(), strstr(), and strrev() in C programming to perform operations like comparing strings, concatenating strings, copying strings, finding string lengths, copying parts of strings, finding substrings, and reversing strings. It also shows how to perform some of these operations like comparison, copying and length calculation without using string handling functions.

Uploaded by

Naresh Babu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Strings Comparison Programs

The document provides examples of using string handling functions like strcmp(), strcat(), strcpy(), strlen(), strncpy(), strncmp(), strncat(), strstr(), and strrev() in C programming to perform operations like comparing strings, concatenating strings, copying strings, finding string lengths, copying parts of strings, finding substrings, and reversing strings. It also shows how to perform some of these operations like comparison, copying and length calculation without using string handling functions.

Uploaded by

Naresh Babu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

String comparison without using string handling functions: #include<stdio.h> #include<conio.

h> void main() { char str[10],str1[10]; int i=0; clrscr(); printf("Enter string1: "); gets(str); printf("Enter string2:"); gets(str1); while(str[i]==str1[i]) /*&& str[i]!='\0' && str1[i]!='\0')*/ i++; if(str[i]=='\0' && str1[i]=='\0') printf("Strings are equal"); else printf("Strings are not equal"); getch(); } String comparison using string handling functions: #include<stdio.h> #include<conio.h> void main() { char str1[10],str2[10]; int x; clrscr(); printf("Enter sring1:"); gets(str1); printf("Enter string2:"); gets(str2); x=strcmp(str1,str2); if(x==0) printf("Both strings are equal"); else printf("Strings are not equal"); getch(); } String Concatenation using string handling functions: #include<stdio.h> #include<conio.h> void main() {

char str1[10]="very"; char str2[10]="good"; clrscr(); strcat(str1,str2); printf("\nAfter concatenation String1 is:%s",str1); getch(); } Example2: #include<stdio.h> #include<conio.h> void main() { char str1[10]="very"; clrscr(); strcat(str1,"bad"); printf("\nAfter concatenation String1 is:%s",str1); getch(); } Example 3: #include<stdio.h> #include<conio.h> void main() { char str1[20]="Very",str2[5]="good",str3[5]="news"; clrscr(); strcat(strcat(str1,str2),str3); printf("After concatenation string1 is:%s",str1); getch(); } Example 4: #include<stdio.h> #include<conio.h> void main() { char str1[10],str2[5]; clrscr(); printf("Enter string1:"); gets(str1); printf("Enter string2:"); gets(str2); strcat(str1,str2); puts(str1); puts(str2); getch(); }

String Copy using string handling functions: #include<stdio.h> #include<conio.h> void main() { char str1[10]="good",str2[10]="Morning"; clrscr(); strcpy(str1,str2); printf("\n After String copy string1 is:%s",str1); getch(); } String Copy without using string handling functions: #include<stdio.h> #include<conio.h> void main() { char str1[20]="good morning",str2[20]; int i; clrscr(); for(i=0;str1[i]!='\0';i++) str2[i]=str1[i]; str2[i]='\0'; printf("After copying string2 is:%s",str2); getch(); } String length using string handling functions: #include<stdio.h> #include<conio.h> void main() { char str[10]="very good"; int l; clrscr(); l=strlen(str); printf("\n The given string is:%s",str); printf("\nLength of the string is:%d",l); getch(); } String length without using string handling functions: #include<stdio.h> #include<conio.h> void main() { char str[10]="Very good"; int i=0; clrscr();

while(str[i]!='\0') i=i+1; printf("\n The length of the string is:%d",i); getch(); } Strncpy(): #include<stdio.h> #include<conio.h> void main() { char s1[10]="very good",s2[10]; clrscr(); strncpy(s2,s1,4); s2[4]='\0'; printf("String2 is:%s",s2); getch(); } Strncmp() #include<stdio.h> #include<conio.h> void main() { char s1[15]="sitams cse",s2[15]="sitams ece"; int x; clrscr(); x=strncmp(s1,s2,5); if(x==0) printf("First 5 characters are equal"); else printf("Not equal"); getch(); } Strncat(): #include<stdio.h> #include<conio.h> void main() { char s1[20]="Bala",s2[10]="Gurusamy"; clrscr(); strncat(s1,s2,4); printf("After concatenation s1 is:%s",s1); getch(); }

Strstr(): #include<stdio.h> #include<conio.h> void main() { char main[10]="usha rani",sub[5]="rani",*x; clrscr(); x=strstr(main,sub); if(x==NULL) printf("The sub string is not present in main string"); else printf("\nPosition of substring in main string is:%d",(x-main)); getch(); } Strrev(): #include<stdio.h> #include<conio.h> #include<string.h> void main() { char *s1="sitams"; clrscr(); printf("Before reverse s1 is:%s",s1); strrev(s1); printf("\nAfter reverse s1 is:%s",s1); getch(); }

You might also like