In C language, a string is essentially an array of characters. A string is composed of characters and is terminated by a <span>'\0'</span> (null character). Chapter 14 covers basic operations on strings, including input, output, concatenation, comparison, length, and more. Below are some common examples and explanations of string operations.
1. Definition and Initialization of Strings
In C language, strings can be represented using character arrays.
#include <stdio.h>
int main() {
// Define and initialize a string
char str[] = "Hello, World!";
// Output the string
printf("%s\n", str); // Use %s to output the string
return 0;
}
Explanation:
- • The string
<span>"Hello, World!"</span>will automatically append a null character<span>'\0'</span>at the end to indicate the end of the string.
2. String Input and Output
You can use <span>scanf</span> or <span>gets</span> to input strings, but <span>gets</span> is unsafe, so it is recommended to use <span>fgets</span>.
#include <stdio.h>
int main() {
char str[100];
// Use fgets to read a line of string (safer)
printf("Please enter a string:");
fgets(str, sizeof(str), stdin);
// Output the string
printf("The string you entered is: %s", str);
return 0;
}
Explanation:
- •
<span>fgets</span>will read the input string, up to<span>sizeof(str)-1</span>characters, and automatically append a null character<span>'\0'</span>at the end.
3. String Length
To calculate the length of a string, you can use the standard library function <span>strlen</span>, which returns the number of characters in the string (excluding <span>'\0'</span>).
#include <stdio.h>
#include <string.h> // Include string handling library
int main() {
char str[] = "Hello, World!";
// Use strlen function to calculate string length
int length = strlen(str);
printf("The length of the string is: %d\n", length); // Output string length
return 0;
}
Explanation:
- •
<span>strlen</span>function returns the number of characters in the string, excluding the null character<span>'\0'</span>.
4. String Copy
If you want to copy one string to another, you can use the <span>strcpy</span> function.
#include <stdio.h>
#include <string.h> // Include string handling library
int main() {
char str1[] = "Hello, World!";
char str2[50];
// Use strcpy function to copy string
strcpy(str2, str1);
// Output the copied string
printf("The content of str2 is: %s\n", str2);
return 0;
}
Explanation:
- •
<span>strcpy</span>function copies the content of the source string to the destination string.
5. String Concatenation
To concatenate two strings, you can use the <span>strcat</span> function.
#include <stdio.h>
#include <string.h> // Include string handling library
int main() {
char str1[100] = "Hello, ";
char str2[] = "World!";
// Use strcat function to concatenate two strings
strcat(str1, str2);
// Output the concatenated string
printf("The concatenated string is: %s\n", str1);
return 0;
}
Explanation:
- •
<span>strcat</span>function appends the second string to the end of the first string.
6. String Comparison
You can use the <span>strcmp</span> function to compare two strings. If the two strings are the same, <span>strcmp</span> returns 0; if the first string is less than the second, it returns a negative value; if the first string is greater than the second, it returns a positive value.
#include <stdio.h>
#include <string.h> // Include string handling library
int main() {
char str1[] = "Hello";
char str2[] = "hello";
// Use strcmp function to compare two strings
int result = strcmp(str1, str2);
if(result == 0) {
printf("The two strings are the same.\n");
} else if(result < 0) {
printf("The first string is less than the second string.\n");
} else {
printf("The first string is greater than the second string.\n");
}
return 0;
}
Explanation:
- •
<span>strcmp</span>function’s return value indicates the relative order of the two strings.
7. String Search
If you need to find a character or substring in a string, you can use the <span>strchr</span> or <span>strstr</span> functions.
#include <stdio.h>
#include <string.h> // Include string handling library
int main() {
char str[] = "Hello, World!";
// Use strchr to find the position of character 'H'
char *ch = strchr(str, 'H');
if (ch != NULL) {
printf("The position of character 'H' is: %ld\n", ch - str); // Calculate position
} else {
printf("Character 'H' not found.\n");
}
// Use strstr to find the substring "World"
char *sub = strstr(str, "World");
if (sub != NULL) {
printf("The position of substring 'World' is: %ld\n", sub - str);
} else {
printf("Substring 'World' not found.\n");
}
return 0;
}
Explanation:
- •
<span>strchr</span>function finds the first occurrence of a character in a string and returns a pointer to that character. - •
<span>strstr</span>function finds the first occurrence of a substring in a string.
Summary of Common String Functions
| Function | Description |
<span>strlen</span> |
Calculates the length of a string (excluding <span>'\0'</span>). |
<span>strcpy</span> |
Copies one string to another string. |
<span>strcat</span> |
Appends one string to the end of another string. |
<span>strcmp</span> |
Compares the relative order of two strings. |
<span>strchr</span> |
Finds the first occurrence of a character in a string. |
<span>strstr</span> |
Finds the first occurrence of a substring in a string. |
<span>fgets</span> |
Reads a line of string from standard input. |
<span>sprintf</span> |
Formats a string and stores the result in a character array. |
Precautions
- 1. Array Out of Bounds: When manipulating strings, ensure that the destination array has enough space to store the string and its null terminator
<span>'\0'</span>. - 2. Memory Management: When dynamically allocating memory to store strings, ensure to free the memory to avoid memory leaks.
Conclusion
In C language, strings are implemented through character arrays. Learning and mastering common string operations, such as string input and output, concatenation, comparison, searching, etc., is very important for writing C programs. By using the string handling functions provided by C language, various string operations can be performed efficiently.