450 likes | 1.01k Views
STRINGS IN C. Contents. STRINGS Memory Representation Declaring and Initializing string variables String functions Example: String is palindrome or not Result. STRINGS.
E N D
STRINGS IN C
Contents • STRINGS • Memory Representation • Declaring and Initializing string variables • String functions • Example: String is palindrome or not • Result
STRINGS A string is a collection of characters .The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character '\0'. Example: char a[ ]={‘H’,‘E’,‘L’,‘L’,‘O’,‘\0’}; BACK
Memory Representation of string In the memory character of array or string is: It is not necessary to write ‘\0’ when initializing strings.The C compiler automatically places the '\0' at the end of the string when it initializes the array. BACK
Declaring and Initializing string variables Strings can be initialized as follows: char a[6]=“hello”; Or char a[6]={‘H’,‘E’,‘L’,‘L’,‘O’,‘\0’}; Strings can also be initialized without specifying the size: char a[ ]=“hello”; Or char a[ ]={‘H’,‘E’,‘L’,‘L’,‘O’,‘\0’}; BACK
String functions C provide large number of library functions for manipulating strings. these are as follows: • Strlen( ) • Strlcat( ) • Strcpy( ) • Strcmp( ) • Strrev( ) • Strlwr( )
Strlen( ):-This function calculate the length of the string . The syntax is: n=strlen (string); • Strcat( ):-This function appends one string at the end of the another string . The syntax is: strcat (str1,str2); • Strcpy( ):-This function copies a string to another string. The syntax is: strcpy(str1,”hello”);
Strcmp( ):-This function compare two strings. The syntax is: strcmp (str1,str2); • Strrev( ):-This function reverse the string. The syntax is: strrev (Hello); • Strlwr( ):-This function calculate the length of the string.The syntax is: stlwr (str1);
Strupr( ):-This function converts strings to upper string. The syntax is: strupr (str1); NOTE:-These are the standard library functions handled by string. BACK
Example: String is palindrome or not #include<stdio.h> #include<conio.h> #include<string.h> void main() { char s1[20],s2[20]; int n; clrscr(); puts("enter a string..."); gets(s1); n=strlen(s1); printf(“length is=%d\n”,n); strcpy(s2,s1); strrev(s2); if(strcmp(s1,s2)==0) printf("string is palidrome"); else printf("string is not palidrome"); getch(); } BACK
Result BACK
THANKS BACK TO INDEX