Strings can be defined as one-dimensional character arrays terminated by a null character (‘\0’). Character arrays or strings are used to manipulate text, such as words or sentences. Each character in the array occupies one byte of memory, and the last character must always be 0. The terminating character (‘\0’) is important in strings because it is the only way to identify the end of a string. When we define a string as char s[10], the character s[10] is implicitly initialized to a null character in memory.
In C language, there are two ways to declare a string:
-
Using character arrays
-
Using string literals
Let’s look at an example of declaring a string using a character array:
char ch[10] = {'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
As we know, array indexing starts from 0, so as shown in the figure below:
When declaring a string, the size is optional. Therefore, we can write the above code as:
char ch[] = {'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
We can also define a string in C language using string literals, for example:
char ch[] = "javatpoint";
In this case, the compiler automatically adds ‘\0’ at the end of the string.
Difference Between Character Arrays and String Literals
There are two main differences between character arrays and string literals:
-
For character arrays, we need to add the null character ‘\0’ at the end ourselves, while for string literals, the compiler automatically adds it at the end.
-
String literals cannot be reassigned to another set of characters, while character arrays can have their values reassigned.
👇 Click to receive 👇
👉 C Language Knowledge Resource Collection
Example of Strings in C
Let’s look at a simple example where a string is declared and printed. In C language, ‘%s’ is used as the format specifier for strings.
#include <stdio.h>#include <string.h>
int main(){ char ch[11] = {'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'}; char ch2[11] = "javatpoint";
printf("Char Array Value is: %s\n", ch); printf("String Literal Value is: %s\n", ch2);
return 0;}
Output
Char Array Value is: javatpoint
String Literal Value is: javatpoint
Traversing Strings
Traversing strings is one of the most important parts of any programming language. We may need to handle very large texts, which can be done by traversing the text. Traversing a string is different from traversing an integer array. For an integer array, we need to know the length of the array to traverse it, but for strings, we can use the null character (‘\0’) as an end marker.
Here’s a simple example demonstrating how to traverse a string and print each character:
#include <stdio.h>
void printString(char *str) { int i = 0; while (str[i] != '\0') { printf("%c", str[i]); i++; } printf("\n");}
int main() { char ch[] = "Hello, World!"; printString(ch); return 0;}
Output
Hello, World!
This is the basic method for traversing a string. We used a while loop that prints each character until it encounters the null character (‘\0’).
Using String Length
Let’s look at an example that counts the number of vowels in a string.
#include<stdio.h>
void main() { char s[11] = "javatpoint"; int i = 0; int count = 0; while (i < 11) { if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o') { count++; } i++; } printf("The number of vowels %d", count);}
Output
The number of vowels 4
Using Null Character
Let’s see an example that counts the number of vowels using the null character.
#include<stdio.h>
void main() { char s[11] = "javatpoint"; int i = 0; int count = 0; while (s[i] != '\0') { if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o') { count++; } i++; } printf("The number of vowels %d", count);}
Output
The number of vowels 4
Accepting String Input
So far, we have used scanf to accept user input. However, in the case of strings, it can also be used, but there are some different situations. Consider the following code, which stores the string when it encounters a space.
#include<stdio.h>
void main() { char s[20]; printf("Enter the string?"); scanf("%[^
]s", s); printf("You entered %s", s);}
Output
Enter the string?javatpoint is the best
You entered javatpoint is the best
Clearly, the above code will not handle strings separated by spaces.To make this code work for space-separated strings, a small modification to the scanf function is needed, changing scanf(“%s”, s) to scanf(“%[^
]s”, s), which instructs the compiler to store the string s when it encounters a newline character (\n).Let’s consider the following example to store a space-separated string.
#include<stdio.h>
void main() { char s[20]; printf("Enter the string?"); scanf("%[^
]s", s); printf("You entered %s", s);}
Output
Enter the string?javatpoint is the best
You entered javatpoint is the best
Here, we must also note that there is no need to use the address (&) operator in scanf to store strings, because the string s is a character array, and the name of the array s represents the base address of the string (character array), so we do not need to prefix it with &.
Some Important Notes
However, when using scanf to input strings, there are the following points to note:
-
The compiler does not perform boundary checks on character arrays. Therefore, the length of the string may exceed the size of the character array, which may overwrite some important data.
-
We can use gets() instead of scanf, gets() is a built-in function defined in the header file string.h. gets() can only receive one string.
Strings and Pointers
So far, we have used pointers with arrays, functions, and basic data types. However, pointers can also be used to point to strings. Using pointers to point to strings has various advantages. Let’s consider the following example, accessing a string via a pointer.
#include<stdio.h>
void main() { char s[11] = "javatpoint"; char *p = s; // Pointer p points to string s printf("%s", p); // If printing p, it will output the string javatpoint}
Output
javatpoint
We know that strings are character arrays, and pointers can be used in the same way as arrays. In the above example, p is declared as a pointer to the character array s. p and s have the same effect because s is the base address of the string and is treated as a pointer internally. However, we cannot directly change the content of s or copy the content of s directly to another string. For this, we need to use pointers to store the string. In the following example, we demonstrate an example of copying the string content using pointers.
#include<stdio.h>
void main() { char *p = "hello javatpoint"; printf("String p: %s\n", p); char *q; printf("Copying the content of p into q...\n"); q = p; printf("String q: %s\n", q);}
Output
String p: hello javatpoint
Copying the content of p into q...
String q: hello javatpoint
Once a string is defined, it cannot be reassigned to another set of characters. However, using pointers, we can assign a set of characters to a string. Consider the following example.
#include<stdio.h>
void main() { char *p = "hello javatpoint"; printf("Before assigning: %s\n", p); p = "hello"; printf("After assigning: %s\n", p);}
Output
Before assigning: hello javatpoint
After assigning: hello
Programmer Technical Communication Group
Scan the code to enter the group and remember to note: city, nickname, and technical direction.