We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13
Strings
Strings
• Strings are used for storing text/characters.
• The string can be defined as the one- dimensional array of characters terminated by a null ('\0'). • Each character in the array occupies one byte of memory, and the last character must always be \0 (string end). • format specifier : %s Declaration • There are two ways to declare a string in c language. By char array Char c[ 5 ] = {‘P’,’u’,’n’,’n’,’y’}; c[2] =l;
0 1 2 3 4 5
P u n n y \0
size is not mandatory
By string literal char ch[]=“Punny"; Example #include<stdio.h> #include <string.h> int main(){ char ch[10]={'C', 't', 'r', 'a', 'i', 'n', 'i', 'n', 'g', '\0'}; char ch2[10]="ctraining";
printf("Char Array Value is: %s\n", ch);
printf("String Literal Value is: %s\n", ch2); return 0; } String Functions strcpy • The strcpy() function copies the string pointed by source (including the null character) to the destination. • The strcpy() function also returns the copied string. • The strcpy() function is defined in the string.h header file. String Copy #include<stdio.h> #include <string.h> int main(){ char ch[20]={'c', 't', 'r', 'a', 'i', 'n', 'i', 'n', 'g', '\0'}; char ch2[20]; strcpy(ch2,ch); printf("Length of string is: %d,\n",strlen(ch)); printf("Value of second string is: %s",ch2); return 0; } strcmp()
• The strcmp() compares two strings character
by character. If the strings are equal, the function returns 0. Example #include <stdio.h> #include <string.h> int main() { char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; int result; // comparing strings str1 and str2 result = strcmp(str1, str2); printf("strcmp(str1, str2) = %d\n", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %d\n", result); return 0; } gets() function which reads string from the console. #include<stdio.h> #include <string.h> int main(){ char str1[20],str2[20]; printf("Enter 1st string: "); gets(str1);//reads string from console printf("Enter 2nd string: "); gets(str2); if(strcmp(str1,str2)==0) printf("Strings are equal"); else printf("Strings are not equal"); return 0; } C gets() and puts() functions • The gets() and puts() are declared in the header file stdio.h. Both the functions are involved in the input/output operations of the strings. • gets() function • The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space- separated strings. It returns the string entered by the user. • Declaration • char[] gets(char[]); • puts() function • The puts() function is very much similar to printf() function. The puts() function is used to print the string on the console which is previously read by using gets() or scanf() function. Example #include<stdio.h> #include <string.h> int main(){ char name[50]; printf("Enter your name: "); gets(name); //reads string from user printf("Your name is: "); puts(name); //displays string return 0; } • Thanks