String
String
C Strings
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’.
There are two main differences between char array and literal.
•We need to add the null character '\0' at the end of the array by our self
whereas, it is appended internally by the compiler in the case of the string
literal.
#include<stdio.h>
#include <string.h>
int main()
{
char str1[20];
printf("enter a string value :- ");
scanf("%s",str1);
printf("You have entered aString :- %s",str1);
return 0;
}
It is clear from the output that, the above code will not
work for space separated strings.
To make this code working for the space separated strings, the
minor changed required in the scanf function,
scanf("%[^\n]s",s);
which instructs the compiler to store the string s while the new
line (\n) is encountered.
#include<stdio.h>
#include <string.h>
int main()
{
char str1[20];
printf("enter a string value :- ");
scanf("%[^\n]s",str1);
printf("You have entered aString :- %s",str1);
return 0;
}
C String Functions
No. Function Description