C Strings

A string in C is a sequence of characters terminated by a null character (\0). The null character is a special character that has the ASCII value 0. Strings are stored in memory as arrays of characters, but they are treated differently than other arrays.

C String

To declare a string in C, you use the char keyword followed by the name of the string variable and the size of the array. For example, the following code declares a string variable called name that can hold up to 50 characters:

char name[50];

You can also initialize a string variable when you declare it. To do this, you enclose the string literal in double quotes. For example, the following code declares a string variable called greeting and initializes it to the string "Hello, world!"

char greeting[] = "Hello, world!";

To access the characters in a string, you use the square bracket operator ([]). For example, the following code prints the first character of the name string variable:

printf("%c", name[0]);

You can also use the strlen() function to get the length of a string. The strlen() function returns the number of characters in a string, excluding the null terminator. For example, the following code prints the length of the greeting string variable:

int length = strlen(greeting); printf("The length of the string is %d\n", length);

The following code shows a complete example of how to use strings in C:

Full Source | C String
#include <stdio.h> #include <string.h> int main() { // Declaring a string char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // Alternatively, you can declare and initialize a string as follows: char message[] = "Hello"; // Accessing and modifying strings printf("Message: %s\n", message); // Prints "Hello" message[0] = 'J'; // Modifying the first character printf("Modified Message: %s\n", message); // Prints "Jello" // String length int length = strlen(message); // Computes the length of the string printf("Length: %d\n", length); // Prints "5" // String concatenation char destination[50] = "Hello, "; char source[] = "world!"; strcat(destination, source); // Concatenates source to the end of destination printf("Concatenated: %s\n", destination); // Prints "Hello, world!" // String comparison char str1[] = "apple"; char str2[] = "banana"; int result = strcmp(str1, str2); // Compares str1 and str2 if (result < 0) printf("str1 comes before str2.\n"); else if (result > 0) printf("str1 comes after str2.\n"); else printf("str1 and str2 are equal.\n"); // Input and output char input[50]; printf("Enter a string: "); scanf("%s", input); // Reads a string from user input printf("You entered: %s\n", input); return 0; }
//Output: Message: Hello Modified Message: Jello Length: 5 Concatenated: Hello, world! str1 comes before str2.

String functions in C

There are a number of built-in string functions in C. These functions can be used to perform various operations on strings, such as copying, concatenating, and comparing strings.

Here are some examples of built-in string functions in C:

  1. strcpy(): Copies one string to another string.
  2. strcat(): Concatenates two strings.
  3. strcmp(): Compares two strings.
  4. strstr(): Finds the first occurrence of one substring in another string.
  5. strtok(): Breaks a string into tokens.

Conclusion

Strings are typically represented as null-terminated character arrays, with each character followed by a null character ('\0') to mark the string's end. String manipulation is facilitated through standard library functions like strlen, strcpy, and strcat, enabling tasks such as string length determination, copying, concatenation, and comparison. These functions, combined with character arrays, allow C programmers to work with text-based data efficiently.