Lesson 08: Strings
In the Lesson 02: Data Types and Variables, you learned about C/C++ data types. However, did you find that C/C++ does not have a data type of string? This session will discuss the string and its operations in C/C++.
In C/C++ programming, a string is a sequence of characters terminated by a null character ('\0'). Strings are commonly used to represent text data, such as names, phrases, or sentences. Unlike many other programming languages, C/C++ has no built-in string data type. Instead, strings are represented as character arrays, where the last character is always the null character.
8.1 C-Strings (Character Arrays)
C/C++ uses character arrays to store strings instead of defining a new data type for the string. But, there is a subtle difference between character arrays and C-strings.
- A string is a sequence of zero or more characters, and the strings are enclosed in double quotation marks.
- The last character in a C-string is always the NULL character.
- A character array might not contain the NULL character.
- C-strings are stored in one-dimensional character arrays.
What are the differences between 'A' and "A"?
- 'A': is one character, 'A'.
- "A": is a C-string representing two characters: 'A' and '\0'.
- 'A', we need only one memory cell of type char.
- To store "A", we need two memory cells of type char — one for 'A' and one for '\0'.
Similarly, the C-string "Hello" represent six characters: 'H', 'e', 'l', 'l', 'o', and '\0'. To store the C-string "Hello" in computer memory, we need 6 memory cells of type char.
char str[] = "Hello";
When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a NULL character \0 at the end by default.
H | e | l | l | o | \0 |
The actual values stored in the memory are:
72 | 101 | 108 | 108 | 111 | 0 |
Declaring and Allocating Memory for Strings in C
In C programming, strings are represented as character arrays, where each character occupies one element in the array. To declare a string variable, you need to specify the size of the character array using square brackets []. This size indicates the maximum number of characters the string can hold, including the terminating null character (\0).
The basic syntax for declaring a string is as follows:
char string_name[size];
In the above syntax, string_name is any name given to the string variable, and size is used to define the length of the string, i.e., the number of characters strings will store. Please keep in mind that there is an extra terminating character, which is the NULL character \0 used to indicate the termination of a string, which differs strings from normal character arrays.
For example, declare a string to store 20 characters:
char str[21];
Initialize Strings
A string can be initialized in different ways. We will explain this with the help of an example. Below is an example of declaring a string named str and initializing it with "Apple".
char str[] = "Apple"; char str[6] = "Apple"; char str[] = {'A', 'p', 'p', 'l', 'e', '\0'}; char str[6] = {'A', 'p', 'p', 'l', 'e', '\0'}; char *str = "Apple";
Below is the memory representation of a string "Apple":
Index⇒ | str[0] | str[1] | str[2] | str[3] | str[4] | str[5] |
str | A | p | p | l | e | \0 |
Memory Address⇒ | $1000 | $1001 | $1002 | $1003 | $1004 | $1005 |
Actual Value in Memory ⇒ | 65 | 112 | 112 | 108 | 101 | 0 |
Assign Values to Strings
String Input and Output
String input and output operations are fundamental to C programming, allowing you to interact with users and display text information. C provides various functions for reading and writing strings from the standard input (keyboard) and output (console).
String Input with scanf()
String Input with scanf()
The scanf() function is commonly used for reading formatted input from the standard input stream. To read a string using scanf(), you specify the format specifier "%s" and the address of the string variable. For example, to read a string into a variable named message:
char message[20]; scanf("%s", message);
This statement will read a string from the keyboard and store it in the message array.
String Input with gets()
String Input with gets()
The gets() function is another option for reading a string from the standard input. It reads a complete input line up to the first newline character (\n) and stores it in the specified string variable. For instance, to read a line of text into the message:
char message[20]; gets(message);
This statement will read a line of input from the keyboard, including spaces and newline characters, and store it in the message.
String Output with printf()
String Output with printf()
The printf() function is used for formatted output to the standard output stream. To print a string using printf(), you specify the format specifier "%s" and the string variable. For example, to print the contents of message:
printf("%s\n", message);
This statement will print the string stored in message to the console, followed by a newline character (\n).
String Output with puts()
String Output with puts()
The puts() function is another option for printing a string to the standard output. It prints the specified string followed by a newline character. For instance, to print message:
puts(message);
This statement will print the string stored in message to the console, followed by a newline character.
Example Program
Here's an example program that demonstrates reading a string from the user and printing it back:
#include <stdio.h> int main() { char name[20]; printf("Enter your name: "); scanf("%s", name); printf("Hello, %s!\n", name); return 0; }
This program prompts the user to enter their name, reads the input using scanf(), and then greets the user by name using printf().
String input and output operations are essential components of C programming, enabling user interaction and text-based communication. Understanding how to read strings from the keyboard and print strings to the console using functions like scanf(), gets(), printf(), and puts() is crucial for developing interactive and informative C programs.
2
3
8.2 <string.h> Function
The C <string.h> header file declares a set of functions to work strings.
strlen()
The strlen() function calculates the length of a given string.
The strlen() function takes a string as an argument and returns its length. The returned value is of type size_t (the unsigned integer type).
Note that the strlen() function doesn't count the NULL character \0 while calculating the length.
strcmp()
The strcmp() function compares two strings and returns 0 if both strings are identical.
strcpy()
Note: When you use strcpy(), the size of the destination string should be large enough to store the copied string. Otherwise, it may result in undefined behavior.
strcat()
Note: When we use strcat(), the size of the destination string should be large enough to store the resultant string. If not, we will get the segmentation fault error.
8.3 C++ String Class
8.4
8.5
8.6