String in C
String in C Programming
String is a collection of character or group of character, it is achieve in C language by using array character. The string in C language is one-dimensional array of character which is terminated by a null character '\0'. In other words string is a collection of character which is enclose between double cotes ( " " ).
Note: Strings are always enclosed within double quotes. Whereas, character is enclosed within single quotes in C.
Declaration of String in C
Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type.
Example
char s[5];
Initializing Array string
String are initialize into various way in c language;
Example of String in C
char str[]="abcd"; OR char str[5]="abcd"; OR char str[5]={'a','b','c','d','\0'}; OR char str[]={'a','b','c','d','\0'}; OR char str[5]={'a','b','c','d','\0'};
In c language string can be initialize using pointer.
Example
char *c="abcd";
Reading String from user
Example
char str[5]; scanf("%s",&str);
Example of String in C
#include<stdio.h> #include<conio.h> void main() { char str[10]; printf("Enter name: "); scanf("%s",name); printf("Your name is: %s.",name); getch(); }
Example of reading string
Enter name: Hitesh kumar Your name is: Hitesh
Note: String variable str can only take only one word. It is because when white space is encountered, the scanf() function terminates. to over come this problem you can use gets() function.
Syntax
char str[5]; gets(str);
gets()
gets() are used to get input as a string from keyword, using gets() we can input more than one word at a time.
puts()
puts() are used to print output on screen, generally puts() function are used with gets() function.
Example of String program in C
#include<stdio.h> #include<conio.h> void main() { char str[10]; printf("Enter any string: "); gets(str); printf("String are: "); puts(str); getch(); }
Explanation: Here gets() function are used for input string and puts() function are used to show string on console or monitor.
Output
Enter String: hello word String are: hello word
C Library String functions
All the library function of String is available in String.h header file.
S.N. | Function | Purpose |
---|---|---|
1 | strcpy(s1, s2) | Copies string s2 into string s1. |
2 | strcat(s1, s2) | Concatenates string s2 onto the end of string s1. |
3 | strlen(s1) | Returns the length of string s1. |
4 | strcmp(s1, s2) | Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. |
5 | strchr(s1, ch) | Returns a pointer to the first occurrence of character ch in string s1. |
6 | strstr(s1, s2) | Returns a pointer to the first occurrence of string s2 in string s1. |
Important points for Declaration of string
- In declaration of string size must be required to mention otherwise it gives an error.
Syntax
char str[]; // Invalid char str[10]; // Valid
- In declaration of the string size must be unsigned integer value (not -ve or zero value) which is greater than zero only.
Example
char str[]; // Invalid char str[0]; // Invalid char str[-1]; // Invalid char str[10]; // Valid
Syntax
char variable_name[SIZE];
char str[5];
Important points for Initialization of the string
- In Initialization of the string if the specific number of character are not initialized it then rest of all character will be initialized with NULL.
Example
char str[5]={'5','+','A'}; str[0]; ---> 5 str[1]; ---> + str[2]; ---> A str[3]; ---> NULL str[4]; ---> NULL
- In initialization of the string we can not initialized more than size of string elements.
Example
char str[2]={'5','+','A','B'}; // Invalid
- In initialization of the string the size is optional in this case how many variable elements are initialized it, that array element will created.
Example
char str[]={'5','+','A','B'}; // Valid sizeof(str) --> 4byte
When we are working with character array explicitly NULL character does not occupies any physical memory at the end of the character array.
Example
char str[]={'h','e','l','l','o'}; sizeof(str) --> 5byte
String data at the end of the string NULL character occupies physical memory.
Example
char str[]="hello"; sizeof(str) --> 6 byte