In C, there are several standard library functions for manipulating strings. Here are some commonly used string handling functions:
1. `strlen`: This function is used to get the length of a string. It takes a string as input and returns the number of characters in the string, excluding the terminating null character.
```c
char message[] = "Hello, world!";
int length = strlen(message);
printf("The length of the string is %d\n", length);
```
Output: `The length of the string is 13`
2. `strcpy`: This function is used to copy one string to another. It takes two arguments: the destination string and the source string. The destination string must be large enough to hold the entire source string, including the terminating null character.
```c
char source[] = "Hello, world!";
char destination[20];
strcpy(destination, source);
printf("The copied string is %s\n", destination);
```
Output: `The copied string is Hello, world!`
3. `strcat`: This function is used to concatenate two strings. It takes two arguments: the destination string and the source string to be appended to the destination string. The destination string must be large enough to hold both strings, including the terminating null character.
```c
char message1[] = "Hello, ";
char message2[] = "world!";
char result[20];
strcpy(result, message1);
strcat(result, message2);
printf("The concatenated string is %s\n", result);
```
Output: `The concatenated string is Hello, world!`
4. `strcmp`: This function is used to compare two strings. It takes two arguments: the first string and the second string. It returns an integer that indicates the lexicographic relationship between the two strings: 0 if they are equal, a negative value if the first string is less than the second string, or a positive value if the first string is greater than the second string.
```c
char string1[] = "apple";
char string2[] = "banana";
int result = strcmp(string1, string2);
if (result < 0) {
printf("%s comes before %s\n", string1, string2);
} else if (result > 0) {
printf("%s comes after %s\n", string1, string2);
} else {
printf("%s and %s are equal\n", string1, string2);
}
```
Output: `apple comes before banana`
5. `strtok`: This function is used to tokenize a string. It takes two arguments: the string to be tokenized and a string that contains a list of delimiters. The function returns a pointer to the next token in the string, or NULL if there are no more tokens. The function modifies the original string by replacing the delimiter characters with null characters.
```c
char sentence[] = "The quick brown fox jumps over the lazy dog.";
char* token = strtok(sentence, " ");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, " ");
}
```
Output:
```
The
quick
brown
fox
jumps
over
the
lazy
dog.
```
Note that `strtok` modifies the original string, so if you need to keep the original string intact, you should make a copy of it first.
In C, we can find the length of a string using the `strlen()` function. Here's an example:
```
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
int length = strlen(str);
printf("The length of the string is %d\n", length);
return 0;
}
```
In this program, we first declare a character array `str` to store the input string. We then prompt the user to enter a string using the `scanf()` function. Next, we use the `strlen()` function to find the length of the string and store it in the variable `length`. Finally, we print the length of the string using `printf()`.
Note that the `strlen()` function returns the number of characters in the string, excluding the null character (`\0`) at the end of the string.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc