String is a collection of characters terminated by a null character. It can be declared using char array and initialized using double quotes. Various string functions like strlen, strcpy, strcat, strrev, strcmp are used to operate on strings. Strlen returns length of string. Strcpy copies one string to another. Strcat concatenates two strings. Strrev reverses a string. Strcmp compares two strings and returns 0 if equal.
String is a collection of characters terminated by a null character. It can be declared using char array and initialized using double quotes. Various string functions like strlen, strcpy, strcat, strrev, strcmp are used to operate on strings. Strlen returns length of string. Strcpy copies one string to another. Strcat concatenates two strings. Strrev reverses a string. Strcmp compares two strings and returns 0 if equal.
String is a collection of characters terminated by a null character. It can be declared using char array and initialized using double quotes. Various string functions like strlen, strcpy, strcat, strrev, strcmp are used to operate on strings. Strlen returns length of string. Strcpy copies one string to another. Strcat concatenates two strings. Strrev reverses a string. Strcmp compares two strings and returns 0 if equal.
String is a collection of characters terminated by a null character. It can be declared using char array and initialized using double quotes. Various string functions like strlen, strcpy, strcat, strrev, strcmp are used to operate on strings. Strlen returns length of string. Strcpy copies one string to another. Strcat concatenates two strings. Strrev reverses a string. Strcmp compares two strings and returns 0 if equal.
• A string is nothing but a collection of characters in a linear sequence.
• 'C' always treats a string as single data even though it contains whitespaces. • A single character is defined using single quote representation like ‘A’. • A string is represented using double quote marks like “section-C”. • String is terminated with ‘\0’ (Null character) . • NULL character (\0) will automatically be inserted at the end of the string. • '\0' represents the end of the string. • It is also referred as String terminator & Null Character. • This is only character whose ASCII value is 0(Zero). The general syntax for declaring a variable as a string is as follows,
char string_variable_name [array_size];
The general syntax of initialization of string is
char string_name[string_length] = "string";
For Example char ch[12]={‘H', ‘e', ‘l', ‘l', ‘o’, ‘ ‘, ‘W', ‘o', ‘r', ‘l’ ,’d’, '\0'};
H e l l o W o r d D ‘\0’ Input character By using %c
• We use %c for insert single character if we insert
any string using %c then we should use for loop If character in input by using %c and input single character every time . we should know the exact Example length of character void main() { char name[15]; for (i=0 ; i< 14; i++) scanf(“%c”,&name[i]) } Input character By using %s Input character By using gets()
• We also gets() for insert string
• We also use %s for insert string Example Example void main() void main() { { char name[15]; gets(name) char name[15]; puts(name) scanf(“%s”,name) } } using gets() we are able to enter if we insert any string using %s multiple string and print same by using puts() then we only print single string String functions strcpy() The strcpy() function copies the contents of strlen() one string to the another string . The strlen() function calculates the length of a given Syntax: string. strcpy(char* destination, const char* source); The strlen() function takes a string as an argument and The strcpy() function copies the string pointed by returns its length. The returned value is of type long int. source (including the null character) to the Syntax: character array destination. int x = strlen (string) Output: #include <stdio.h> #include <stdio.h> Length of string a = 7 #include <string.h> #include <string.h> Output: Length of string b = 7 void main() void main() awesome { { well char str1[10]= "awesome"; long int x; char str2[10]; char a[20]="Program"; char str3[10]; char b[20]={'P','r','o','g','r','a','m','\0’}; strcpy(str2, str1); x= strlen(b); strcpy(str3, "well"); printf("Length of string a = %ld \n",strlen(a)); puts(str2); printf("Length of string b = %ld \n",x); puts(str3); } getch(); } strrev() strcat() strrev( ) function reverses a given string strcat( ) function is used to concatenates two itself in C language. given strings. Syntax: Syntax . char *strrev(char *string); strcat ( char * destination, const char * source ); Example: #include<stdio.h> strcat ( str2, str1 ); – str1 is concatenated at the end of str2. #include<string.h> strcat ( str1, str2 ); – str2 is concatenated at the end of str1. void main() { ▪ As you know, each string in C is ended up with null char name[30] = "Hello"; character (‘\0’). printf("String before strrev( ) : %s\n",name); ▪ In strcat( ) operation, null character of destination printf("String after strrev( ) : %s",strrev(name)); string is overwritten by source string’s first character getch(); and null character is added at the end of new } destination string which is created after strcat( ) OUTPUT: operation. String before strrev( ) : Hello String after strrev( ) : olleH #include <stdio.h> strcmp() #include <string.h> The strcmp() function compares two strings and void main( ) returns 0 if both strings are identical. { Syntax char str1[ ] = " Computer" ; int x = strcmp (const char* str1, const char* str2); char str2[ ]= " Science" ; The strcmp() function takes two strings and printf ( "\nstring 1 = %s", str1 ) ; returns an integer. printf ( "\n string 2= %s", str2 ) ; strcat ( str1, str2 ) ; printf ( "\nstring after strcat( ) = %s", str1 ) ; The strcmp() compares two strings character by } character.
OUTPUT: If the first character of two strings is equal, then
string 1 = Computer next character of two strings are compared. This string 2 = Science continues until the corresponding characters of string after strcat( ) = Computer Science two strings are different or a null character '\0' is reached. Return Value from strcmp() Return Value Remarks 0 if both strings are identical (equal) negative if the ASCII value of the first unmatched character is less than second. positive integer if the ASCII value of the first unmatched character is greater than second. #include <stdio.h> #include <string.h> int main() { char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; Output int result; // comparing strings str1 and str2 strcmp(str1, str2) = 32 result = strcmp(str1, str2); strcmp(str1, str3) = 0 printf("strcmp(str1, str2) = %d\n", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %d\n", result); getch(); }