It is a sequence of character enclose in double quotes are called as string.
Declaring stringchar str[20] =â€saplogicâ€;
Char str[20] ={‘s’,’a’,’p’,’l’,’o’,’g’,’i’,’c’,’\0’};String handling function
By using strlen() function we find the length of character.
EX: WAP to calculate length of string#include #include int main(){ char a[20]="Ashish"; char b[20]={'A','s','h','i','s','h','\0'}; char c[20]; printf("Enter string: "); gets(c); printf("Length of string a=%d \n",strlen(a)); //calculates length of string before null charcter printf("Length of string b=%d \n",strlen(b)); printf("Length of string c=%d \n",strlen(c)); getch(); return 0; }Ex:WAP to find length without string library function
#include<stdio.h> #include<conio.h> void mystrlen(char *); main() { char str1[20]; clrscr(); printf("\n Enter Any String : "); gets(str1); mystrlen(str1); getch(); } void mystrlen(char *s1) { int length=0; while(*s1!='\0') { length++; s1++; } printf("Length is %d ",length); }o/p
Length is 4
By using strcpy() function we copy one string from one location to another locatrion.
Ex: WAP for copy string from one location to another location#include #include #include int main(){ char a[10],b[10]; printf("Enter string: "); gets(a); strcpy(b,a); // String a is copied to string b. printf("Copied string is : "); puts(b); getch(); return 0; }WAP to copy string without string function
#include<stdio.h> #include<conio.h> void mystrcpy(char *,char *); main() { char str1[20],str2[20]; clrscr(); printf("\n Enter Any String : "); gets(str1); mystrcpy(str1,str2); printf("After Copied "); puts(str2); getch(); } void mystrcpy(char *s1,char *s2) { while(*s1!='\0') { *s2=*s1; s1++;s2++; } *s2='\0'; }o/p
Enter any string: prashant After copied Prashant
By using string concatenation strcat() function we concatenate two string that is we add two string.
Ex: WAP to concat two string#include #include #include int main(){ char str1[]="This is ", str2[]="help4code.com"; strcat(str1,str2); puts(str1); puts(str2); getch(); return 0; }WAP to concate two strings without string function
#include<stdio.h> #include<conio.h> void mystrcat(char *,char *); main() { char str1[20],str2[20]; clrscr(); printf("\n Enter First String : "); gets(str1); printf("\n Enter Second String : "); gets(str2); mystrcat(str1,str2); printf("After concatenation "); puts(str1); getch(); } void mystrcat(char *s1,char *s2) { while(*s1!='\0') { s1++; } while(*s2!='\0') { *s1=*s2; s1++;s2++; } *s1='\0'; }o/p
Enter First String : prashant Enter Second String:jha After concatenation Prashantjha
By using strcmp() string comparison function we compare two string.
Ex: WAP for string comparision#include <stdio.h> #include <string.h> #include <conio.h> int main(){ char str1[20],str2[20]; printf("Enter first string: "); gets(str1); printf("Enter second string: "); gets(str2); if(strcmp(str1,str2)==0) printf("\nBoth strings are equal"); else printf("\nStrings are not equal"); getch(); return 0; }WAP to compare string without string function
#include<stdio.h> #include<conio.h> void mystrcmp(char *,char *); main() { char str1[20],str2[20]; clrscr(); printf("\n Enter First String : "); gets(str1); printf("\n Enter Second String : "); gets(str2); mystrcmp(str1,str2); getch(); } void mystrcmp(char *s1,char *s2) { int flag=1; while(*s1!='\0' || *s2!='\0') { if(*s1!=*s2) flag=0; s1++;s2++; } if(flag==1) puts("\n Strings are matched"); else puts("\n Strings are not matched"); }o/p
Enter First String :jha Enter Second String :jha Strings are matched
It is a string upper case function By using it we can convert lower case character to upper case character.
Ex: WAP to convert lower case to upper case#include <stdio.h> #include <string.h> #include <conio.h> int main(){ char str1[]="Ashish Gadpayle"; puts(strupr(str1)); getch(); return 0; }WAP to convert string into uppercase without string function
#include<stdio.h> #include<conio.h> void mystrupr(char *s); main() { char ch[20]; clrscr(); printf("\n Enter any String : "); gets(ch); mystrupr(ch); printf("\n Upper case String is "); puts(ch); getch(); } void mystrupr(char *s) { while(*s!='\0') { *s=*s-32; s++; } }o/p
Enter any String : SSS Upper case String is:333
It is a string lower case function
Ex: WAP for lower case function#include <stdio.h> #include <string.h> #include <conio.h> int main(){ char str1[]="ASHISH GADPAYLE"; puts(strlwr(str1)); getch(); return 0; }WAP to convert string into lowercase without string function
#include<stdio.h> #include<conio.h> void mystrlwr(char *s); main() { char ch[20]; clrscr(); printf("\n Enter any String : "); gets(ch); mystrlwr(ch); printf("\n Upper case String is "); puts(ch); getch(); } void mystrlwr(char *s) { while(*s!='\0') { *s=*s+32; s++; } }o/p
Enter any String : SSS Upper case String is:sss
It is a string reverse function By using it we can reverse the function.
Ex: WAP for reverse string#include<stdio.h> #include<conio.h> main() { char str1[20],rev[20]; int n,i=0,j; clrscr(); printf("\n Enter any String : "); gets(str1); n=strlen(str1); j=n-1; while( i>=0 && iOutput
Enter any String : jha Reverse String is: ahj