C Strings: 'J' 'A' 'V' 'A' 'T' 'P' 'O' 'I' 'N' 'T' '/0'
C Strings: 'J' 'A' 'V' 'A' 'T' 'P' 'O' 'I' 'N' 'T' '/0'
1. By char array
2. By string literal
1. char ch[10]={'j', 'a', 'v', 'a', 't', 'p',, 'o', 'i', 'n', 't', '\0'};
As we know, array index starts from 0, so it will be represented as in the figure given below.
While declaring string, size is not mandatory. So we can write the above code as given below:
We can also define the string by the string literal in C language. For example:
1. char ch[]="javatpoint";
There are two main differences between char array and literal.
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
5. char ch2[11]="javatpoint";
6.
7. printf("Char Array Value is: %s\n", ch);
8. printf("String Literal Value is: %s\n", ch2);
9. return 0;
10. }
Output
Traversing String
Traversing the string is one of the most important aspects in any of the programming languages.
We may need to manipulate a very large text which can be done by traversing the text.
Traversing string is somewhat different from the traversing an integer array. We need to know
the length of the array to traverse an integer array, whereas we may use the null character in the
case of string to identify the end the string and terminate the loop.
1. #include<stdio.h>
2. void main ()
3. {
4. char s[11] = "javatpoint";
5. int i = 0;
6. int count = 0;
7. while(i<11)
8. {
9. if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
10. {
11. count ++;
12. }
13. i++;
14. }
15. printf("The number of vowels %d",count);
16. }
Output
Let's see the same example of counting the number of vowels by using the null character.
1. #include<stdio.h>
2. void main ()
3. {
4. char s[11] = "javatpoint";
5. int i = 0;
6. int count = 0;
7. while(s[i] != NULL)
8. {
9. if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
10. {
11. count ++;
12. }
13. i++;
14. }
15. printf("The number of vowels %d",count);
16. }
Output
1. #include<stdio.h>
2. void main ()
3. {
4. char s[20];
5. printf("Enter the string?");
6. scanf("%s",s);
7. printf("You entered %s",s);
8. }
Output
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, i.e.,
instead of writing scanf("%s",s), we must write: scanf("%[^\n]s",s) which instructs the compiler to store the
string s while the new line (\n) is encountered. Let's consider the following example to store the space-
separated strings.
1. #include<stdio.h>
2. void main ()
3. {
4. char s[20];
5. printf("Enter the string?");
6. scanf("%[^\n]s",s);
7. printf("You entered %s",s);
8. }
Output
Here we must also notice that we do not need to use address of (&) operator in scanf to store a
string since string s is an array of characters and the name of the array, i.e., s indicates the base
address of the string (character array) therefore we need not use & with it.
Some important points
However, there are the following points which must be noticed while entering the strings by
using scanf.
o The compiler doesn't perform bounds checking on the character array. Hence, there can be a case
where the length of the string can exceed the dimension of the character array which may always
overwrite some important data.
o Instead of using scanf, we may use gets() which is an inbuilt function defined in a header file
string.h. The gets() is capable of receiving only one string at a time.
1. #include<stdio.h>
2. void main ()
3. {
4. char s[11] = "javatpoint";
5. char *p = s; // pointer p is pointing to string s.
6. printf("%s",p); // the string javatpoint is printed if we print p.
7. }
Output
javatpoint
As we know that string is an array of characters, the pointers can be used in the same way they
were used with arrays. In the above example, p is declared as a pointer to the array of characters
s. P affects similar to s since s is the base address of the string and treated as a pointer internally.
However, we can not change the content of s or copy the content of s into another string directly.
For this purpose, we need to use the pointers to store the strings. In the following example, we
have shown the use of pointers to copy the content of a string into another.
1. #include<stdio.h>
2. void main ()
3. {
4. char *p = "hello javatpoint";
5. printf("String p: %s\n",p);
6. char *q;
7. printf("copying the content of p into q...\n");
8. q = p;
9. printf("String q: %s\n",q);
10. }
Output
1. #include<stdio.h>
2. void main ()
3. {
4. char *p = "hello javatpoint";
5. printf("Before assigning: %s\n",p);
6. p = "hello";
7. printf("After assigning: %s\n",p);
8. }
Output
C gets() function
The gets() function enables the user to enter some characters followed by the enter key. All the characters
entered by the user get stored in a character array. The null character is added to the array to make it a
string. The gets() allows the user to enter the space-separated strings. It returns the string entered by the
user.
Declaration
1. char[] gets(char[]);
The gets() function is risky to use since it doesn't perform any array bound checking and keep reading the
characters until the new line (enter) is encountered. It suffers from buffer overflow, which can be avoided
by using fgets(). The fgets() makes sure that not more than the maximum limit of characters are read.
Consider the following example.
1. #include<stdio.h>
2. void main()
3. {
4. char str[20];
5. printf("Enter the string? ");
6. fgets(str, 20, stdin);
7. printf("%s", str);
8. }
Output
Enter the string? javatpoint is the best website
javatpoint is the b
C puts() function
The puts() function is very much similar to printf() function. The puts() function is used to print the string
on the console which is previously read by using gets() or scanf() function. The puts() function returns an
integer value representing the number of characters being printed on the console. Since, it prints an
additional newline character with the string, which moves the cursor to the new line on the console, the
integer value returned by puts() will always be equal to the number of characters present in the string plus
1.
Declaration
1. int puts(char[])
Let's see an example to read a string using gets() and print it on the console using puts().
1. #include<stdio.h>
2. #include <string.h>
3. int main(){
4. char name[50];
5. printf("Enter your name: ");
6. gets(name); //reads string from user
7. printf("Your name is: ");
8. puts(name); //displays string
9. return 0;
10. }
Output:
Enter your name: Sonoo Jaiswal
Your name is: Sonoo Jaiswal