String Length - STRLEN
/* STRING LENGTH - STRLEN*/
#include<stdio.h> int main() { char str[100]; int i; printf("Please enter a string: "); // gets(str); // fgets is a better option over gets to read multiword string . fgets(str, 100, stdin); // Following can be added for extra precaution for '\n' character // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL; for(i=0;str[i]!=NULL;i++); printf("Length of string is: %d",i); return 0; }
String to Upper Case - STRUPR
/* STRING TO UPPER CASE - STRUPR */
#include<stdio.h> int main() { char str[100]; int i; printf("Please enter a string: "); // gets(str); // fgets is a better option over gets to read multiword string . fgets(str, 100, stdin); // Following can be added for extra precaution for '\n' character // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL; for(i=0;str[i]!=NULL;i++) { if(str[i]>='a'&&str[i]<='z') str[i]-=32; } printf("String in upper case is: %s",str); return 0; }
String to lower case - STRLWR
/* STRING TO LOWER CASE - STRLWR */ #include<stdio.h> int main() { char str[100]; int i; printf("Please enter a string: "); // gets(str); // fgets is a better option over gets to read multiword string . fgets(str, 100, stdin); // Following can be added for extra precaution for '\n' character // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL; for(i=0;str[i]!=NULL;i++) { if(str[i]>='A'&&str[i]<='Z') str[i]+=32; } printf("String in lower case is: %s",str); return 0; }
Toggle case of string
/* TOGGLE CASE OF STRING */ #include<stdio.h> int main() { char str[100]; int i; printf("Please enter a string: "); // gets(str); // fgets is a better option over gets to read multiword string . fgets(str, 100, stdin); // Following can be added for extra precaution for '\n' character // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL; for(i=0;str[i]!=NULL;i++) { if(str[i]>='A'&&str[i]<='Z') str[i]+=32; else if(str[i]>='a'&&str[i]<='z') str[i]-=32; } printf("String in toggle case is: %s",str); return 0; }
String Compare - STRCMP
/* STRING COMPARE - STRCMP */ #include<stdio.h> int main() { char str1[100], str2[100]; int i,d=0; printf("Please enter first string: "); // gets(str1); // fgets is a better option over gets to read multiword string . fgets(str1, 100, stdin); // Following can be added for extra precaution for '\n' character // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL; printf("Please enter second string: "); // gets(str2); fgets(str2, 100, stdin); // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL; for(i=0;str1[i]!=NULL&&str2[i]!=NULL;i++) { if(str1[i]!=str2[i]) break; } d=str1[i]-str2[i]; if(d==0) printf("Strings are equal"); else printf("Strings are not equal"); return 0; }
String Compare Ignore Case - STRICMP
/* STRING COMPARE IGNORE CASE - STRICMP */ #include<stdio.h> int main() { char str1[100], str2[100]; int i, d=0, flag=0; char x,y; printf("Please enter first string: "); // gets(str1); // fgets is a better option over gets to read multiword string . fgets(str1, 100, stdin); // Following can be added for extra precaution for '\n' character // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL; printf("Please enter second string: "); // gets(str2); fgets(str2, 100, stdin); // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL; for(i=0;str1[i]!=NULL&&str2[i]!=NULL;i++) { x=str1[i]; y=str2[i]; if(x>='a'&&x<='z') x-=32; if(y>='a'&&y<='z') y-=32; if(x!=y) { break; flag=1; } } if(flag=0) { x=str1[i]; y=str2[i]; if(x>='a'&&x<='z') x-=32; if(y>='a'&&y<='z') y-=32; } d=x-y; if(d==0) printf("Strings are equal"); else printf("Strings are not equal"); return 0; }
String Reverse - STRREV
/* STRING REVERSE - STRREV */ #include<stdio.h> int main() { char str[100]; int i,j; char c; printf("Please enter a string: "); // gets(str); // fgets is a better option over gets to read multiword string . fgets(str, 100, stdin); // Following can be added for extra precaution for '\n' character // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL; for(i=0;str[i];i++); for(j=0,i--;j<i;j++,i--) { c=str[i]; str[i]=str[j]; str[j]=c; } printf("Reversed string is: %s",str); return 0; }
String Concatenation - STRCAT
/* STRING CONCATENATION - STRCAT */ #include<stdio.h> int main() { char str1[500], str2[100]; int i, j; printf("Please enter first string: "); // gets(str); // fgets is a better option over gets to read multiword string . fgets(str1, 500, stdin); // Following can be added for extra precaution for '\n' character // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL; printf("Please enter second string: "); // gets(str2); fgets(str2, 100, stdin); // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL; for(i=0;str1[i]!=NULL;i++); for(j=0;str2[j]!=NULL;j++) { str1[i++]=str2[j]; } str1[i]=NULL; printf("Concatenated string is: %s",str1); return 0; }
Locate a character in String - STRCHR
/* Locate a character in String - STRCHR */ #include<stdio.h> int main() { char str[500]; int i; char ch; printf("Please enter your string: "); // gets(str); // fgets is a better option over gets to read multiword string . fgets(str, 500, stdin); // Following can be added for extra precaution for '\n' character // if(str[length(str)-1] == '\n') str[strlen(str)-1]=NULL; printf("Please enter a character: "); getchar(ch); for(i=0;str[i]!=NULL;i++) { if(str[i]==ch) break; } if(str[i] == NULL) printf("%c not found in %s",ch,str); else printf("\"%c\" found in \"%s\" at %d location",ch,str,i+1); return 0; }
Locate a word in String - STRSTR
/* Locate a word in String - STRSTR */ #include<stdio.h> int main() { char str1[100], str2[100]; int i,j,k; printf("Please enter first string: "); // gets(str1); // fgets is a better option over gets to read multiword string . fgets(str1, 100, stdin); // Following can be added for extra precaution for '\n' character // if(str1[length(str1)-1] == '\n') str1[strlen(str1)-1]=NULL; printf("Please enter a word: "); // gets(str2); fgets(str2, 100, stdin); // if(str2[length(str2)-1] == '\n') str2[strlen(str2)-1]=NULL; for(i=0;str1[i]!=NULL;i++) { for(k=i,j=0;str2[j]!=NULL;j++,k++) if(str1[k]!=str2[j]) break; if(str2[j]==NULL) { printf("\"%s\" is the sub string of \"%s\" at %d location",str2,str1,k+1); i=-1; break; } } if(i!=-1) printf("\"%s\" is not the sub string of \"%s\"",str2,str1); return 0; }
Sort array of 10 strings
/* Sort array of 10 strings. */ #include<string.h> #include<stdio.h> int main() { int i,j; char str[10][10],temp[10]; printf("Type 10 names :\n"); for(i=0;i<10;i++) { // gets(str[i]); // fgets is a better option over gets to read multiword string . fgets(str[i], 10, stdin); } for(i=0;i<10;i++) { for(j=0;j<10-1-i;j++) { if(strcmpi(str[j],str[j+1])>0) { strcpy(temp,str[j]); strcpy(str[j],str[j+1]); strcpy(str[j+1],temp); } } } printf("\nSorted Names :\n"); for(i=0;i<10;i++) puts(str[i]); return 0; }
Just Give Your Feedback ConversionConversion EmoticonEmoticon