As we learnt, the array is the collection of homogeneous data type elements.the data type may be int, char,
when we collect character data type then it is known as an array of characters while if this array of characters is written inside the ” ” instead of [ ] then it is known as string.the last element of an array of character element written inside ” ” is ‘\0’ by default.
Following points makes string different from array-
— The dataType of a string is always char.
— By default, the last element of a string is always a null character(‘\0’).
— String Constant is written in double quote(“<string_Constant>”).
— For complete string input or output, %s data specifier is used.
1 |
char string_name[stringSizeMax+1]; |
Note : +1 for the last default ‘\0’ character in string-literal.
Example
char name[20];
where name = string_name
[20] = input string size must be less than 20 character
String Initialization is a process to assign a value to the string at the time of declaration.Size is not necessary to give if we initialize a string with some constant.
1 |
char string_name[]="string-literal" |
1 |
char str[]="hello"; |
In above string initialization “hello” is a constant string literal and store into read-only memory. and it saves a copy into the stack when it assigns to the ‘str’ array. So all the operation will perform on the stack copy and the change in the string is possible-
1 |
str[0]='M'//correct because this value overwrite at stack copy. |
1 |
char *string_pointer="string-literal" |
1 |
char *str="hello" |
In above string initialization “hello” is a constant string literal and store into read-only memory while str is a pointer to that string means str has the address of a constant string literal which is read-only memory so any write is not
1 |
str[0]='M'//wrong because str pointer points the constant string literal. |
that’s why to avoid confusion always use const in string pointer declartaion like this –
1 |
const char *str="hello";//recommended for the string pointer declaration |
Example:
String Representation : char name[ ]=”Amit”;
Array Representation : char name[ ]={ ‘A’,’m’,’i’,’t’ ,’\0′};
So you can see that in the array we have to insert ‘\0’ character manually while string has its default as the last element of the string.
Although the string is an array of character elements so we can use %c to read or write the character element but we have to use the loop for the number of times because %c will read or write a single character at a time.
%s data specifier saves us from looping and it can read or write a complete string in one go. Actually, it takes the advantage of Null character present in the string
when we use %s then it started to read or write from the file until a null character or space is not found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> int main() { char name[100]; printf("Enter any String without Space : "); scanf("%s",name); /*name=string name= base address of string*/ printf("\nEnterd String is : %s",name); printf("\nEnter any String with Space : "); scanf("%s",name); /*name=string name= base address of string*/ printf("\nEnterd String is : %s",name); return 0; } |
Output:
1 2 3 4 5 6 7 |
Enter Any String Without Space : HelloIndia Entered string is : HelloIndia Enter Any String Without Space : HelloIndia Entered string is : Hello |
As we have seen in above example, The scanf() function terminate the input if space is available in between the string constant.
scanf will terminate string when space is found. In above example, we got only Hello instead of Hello India because scanf will accept the data before space.
So instead of using scanf with the %s specifier for string input, we can use gets() function.it is library defined function(stdio.h).
1 |
char* gets(char *string_ptr); |
string_ptr: pointer to the string or string variable name.
gets() function doesn’t need %s specifier because it only accepts string nothing else.
This function takes the input from stdin file and copies into the addressed buffer(string_ptr) until it doesn’t get the null character.It will not terminate on space like scanf() function.
Return Value:
Success: then it returns a pointer to the input string
Fail: Null Pointer (pointer that doesn’t have any address)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> int main() { char name[100]; printf("Enter any String with or without Space: "); gets(name); /*name=string name= base address of string*/ printf("\nEnterd String is : %s",name); return 0; } |
Output:
1 2 |
Enter any String with or without Space : hello how are you Enterd String is : hello how are you |
Although printf() doesn’t have any drawback like scanf and it can do the same work for
1 |
int puts(char *string_ptr); |
string_ptr: pointer to the string or string variable name.
puts() function doesn’t need %s specifier because it only accepts string nothing else.
puts() function takes the output from the buffer(string_ptr) and copies it to stdout file to display the output on the console.
Return Value:
Success: Number of bytes transfer to the stdout file.
Fail: 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { char name[100]; printf("Enter any String with or without Space: "); gets(name); /*name=string name= base address of string*/ //printf("\nEnterd String is : %s",name); puts(name); return 0; } |
Output:
1 2 |
Enter any String with or without Space : hello how are you Enterd String is : hello how are you |
I have mentioned some important points regarding string representation in below table to avoid the confusion.Look at these and try to apply in any string example to be more clear.