Accessing Characters of a String in C
A string is an array of characters. For this reason, it is possible to access individual characters of the string using the indexing operator [] or pointer arithmetic.
In this lesson we will see practical examples that will show us how to access individual characters of a string in C Language.
Strings as Arrays
Since a string is, in all respects, an array, it is possible to access individual characters of the string in the same way you access elements of an array, that is by using the indexing operator [].
If you want to process all the characters of a string, you can use a for loop that increments an index variable i and selects the characters of the string through the expression s[i].
Let's take an example; suppose we want to count all the vowels present in a string. We can implement the program in this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
If we try to compile and execute the program we get a result similar to the following:
Insert a string: Hello, world!
The string contains 3 vowels.
The core of the program is the count_vowels() function. This function takes a string as input and returns the number of vowels present in the string. We need to notice some details:
- First of all, the function accepts as input a string to which the
constmodifier has been applied. This means that the function cannot modify the input string. If the function tries to modify its content, the compiler will signal an error. - The
count_vowels()function uses aforloop to scroll through all the characters of the string. The variableiis used as an index to access the characters of the string.
This is not the only way in which it is possible to write the count_vowels function. We can also use pointer arithmetic to scroll through the string. Let's see how to do it:
int count_vowels(const char s[]) {
int n_vowels;
const char *p;
n_vowels = 0;
for (p = s; *p != '\0'; p++) {
if (vowel(*p)) {
n_vowels++;
}
}
return n_vowels;
}
In this second case, rather than using an index i to access the characters of the string, we use a pointer p that points to the first character of the string. The for loop terminates when the pointer p points to the terminator character \0. In this way, the for loop scrolls through all the characters of the string.
An important thing to notice is the fact that the pointer p has been declared as a pointer to const char. This means that, although it cannot be used to modify the string, you can still modify the address it points to. In this way, the pointer p can be incremented to move one character at a time along the string.
Recapping:
A string can be treated as an array of characters.
A string is in all respects an array of characters. For this reason, it is possible to access individual characters of the string using alternatively:
- Operations to access array elements, through the indexing operator
[]; - Pointer arithmetic, through the indirection operator
*.
Examples
Printing a String in Reverse
Suppose we want to write a program that reads a string as input from the keyboard and prints it in reverse. For example, if the user inserts the string Hello, world!, the program must print !dlrow ,olleH.
The core of this program will be the print_in_reverse() function. This function takes a const string as input and uses pointer arithmetic in conjunction with a for loop.
A possible implementation of the print_in_reverse() function is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
The print_in_reverse() function is quite simple. First it scrolls through the string to find the terminator character \0. At the end of the first for loop, the pointer p points to the terminator character \0. Subsequently, the second for loop scrolls through the string in reverse, printing each character until it reaches the first character of the string.
The complete program is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
If we try to compile and execute the program we get a result similar to the following:
Insert a string: Hello, world!
!dlrow ,olleH
Counting Punctuation Symbols
Suppose we want to write a program that reads a string as input and counts the number of punctuation symbols present, including commas, periods, exclamation marks, question marks, etc.
We can implement a count_punctuation() function using pointer arithmetic. Let's see how to do it:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
This function, in turn, uses a punctuation() function that returns a value different from zero if the character passed as a parameter is a punctuation symbol. Let's see how to implement the punctuation() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
The complete program is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
If we try to compile and execute the program we get a result similar to the following:
Insert a string: Hello, world!
The string contains 2 punctuation symbols.
In Summary
In this lesson we have seen that, since a string is an array of characters, we can alternatively use array indexing or pointer arithmetic to access the characters of a string.
Starting from the next lesson we will see the functions that the standard library of the C language makes available to manipulate strings. We will start with how to copy a string into another.