String in C:-
A string in C is a character array terminated with a null character.
All the string are character array but all the character array is not a string.
main() { char p[]={‘a’,’b’,’c’,’d’}; char q[]={‘a’,’b’,’c’,’d’,’\0’}; }
Here p is a character array and q is a string.
Anything is written within a double code that is known as the string constant.
Ex:-
main() { char p[]={‘a’,’b’,’c’,’d’,’\0’}; char q[]=”abcd”; }
Here p and q both are the same but the most appropriate declaration is q.
Here q is string constant.
Here some important string functions are used.
Strlen:-
Strlen measures the length of the string without the null character.
main() { char r[]="abc"; printf("%d %d",strlen(r)); }
Size_of:-
Size_of measure the length of the string including null character.
main() { char r[]="abc"; printf("%d %d", sizeof(r)); }
Strcpy:-
strcpy copies the entire string including null character.
main() { char x[20]; strcpy(x,"c program"); printf("%s",x); }
Strncpy:-
strncpy only copy no. of character except null character.
main()
{ char x[20]; strncpy(x,"c program",4); printf("%s",x); }
Strcmp:-
Strcmp is used to compare two string.
main() { char x[]="Abc"; char y[]="abc"; int k=strcmp(x,y); if(k==0) { printf("same"); } else { printf("different"); } }
Strcat:-
Strcat command is used to concat the string.
main()
{ char x[30]="abc"; char y[20]="def"; strcat(x,y); printf("%s",x); }
11 replies on “String in C”
Really appreciate you sharing this blog. Keep writing.
Thanks for giving the feedback
Muchos Gracias for your blog post.Much thanks again. Keep writing.
Thanx for giving the feedback ..
Major thankies for the article post.Really looking forward to read more. Great.
Thanks for giving the feedback
Thank you ever so for you article post.Really thank you! Keep writing.
Thanks for giving the feedback
Great, thanks for sharing this blog post.Thanks Again. Really Great.
Major thanks for the blog article.Really looking forward to read more. Really Great.
Thanks for giving the feedback