Strings in C :
String :
A string is a collection of characters. Strings are always enlosed in double quotes as "string_constant".
Strings are used in string handling operations such as,
- Counting the length of a string.
- Comparing two strings.
- Copying one string to another.
- Converting lower case string to upper case.
- Converting upper case string to lower case.
- Joining two strings.
- Reversing string.
Declaration :
The string can be declared as follow :
Syntax: char string_nm[size]; Example: char name[50];
String Structure :
When compiler assigns string to character array then it automatically suppliesnull character ('\0') at the end of string. Thus, size of string = original length of string + 1.
Read Strings :
To read a string, we can use scanf() function with format specifier %s.
char name[50];
scanf("%s",name);
The above format allows to accept only string which does not have any blank space, tab, new line, form feed, carriage return.
Write Strings :
To write a string, we can use printf() function with format specifier %s.
char name[50];
scanf("%s",name);
printf("%s",name);
No comments:
Post a Comment