C language content

In C programming, strings are arrays of characters terminated by a null character (\0). This special character indicates the end of the string, distinguishing it from other data arrays. C provides a set of standard library functions to handle strings, which are included in the header file <string.h>.

String Basics

Declaration and Initialization

Strings in C can be declared in several ways:

1. Character array:

				
					char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

				
			

2. String literal:

				
					char str2[] = "Hello";

				
			

String Termination

All C strings are terminated by a null character (\0), which allows functions to determine the end of the string.

String Handling Functions

strlen()

  • Purpose: Returns the length of the string (number of characters before the null character).
  • Syntax: size_t strlen(const char *str);
  • Example:
				
					char str[] = "Hello";
size_t len = strlen(str); // len will be 5

				
			
  • strcpy()

    • Purpose: Copies one string into another.
    • Syntax: char *strcpy(char *dest, const char *src);
    • Example:
				
					char src[] = "Hello";
char dest[6];
strcpy(dest, src); // dest now contains "Hello"

				
			
  • strncpy()

    • Purpose: Copies up to n characters from one string to another.
    • Syntax: char *strncpy(char *dest, const char *src, size_t n);
    • Example:
				
					char src[] = "Hello, World!";
char dest[6];
strncpy(dest, src, 5);
dest[5] = '\0'; // dest now contains "Hello"

				
			
  • strcat()

    • Purpose: Concatenates (appends) one string to the end of another.
    • Syntax: char *strcat(char *dest, const char *src);
    • Example:
				
					char dest[12] = "Hello";
char src[] = " World";
strcat(dest, src); // dest now contains "Hello World"

				
			
  • strncat()

    • Purpose: Appends up to n characters from one string to another.
    • Syntax: char *strncat(char *dest, const char *src, size_t n);
    • Example:
				
					char dest[12] = "Hello";
char src[] = " World!";
strncat(dest, src, 6); // dest now contains "Hello World"

				
			
  • strcmp()

    • Purpose: Compares two strings lexicographically.
    • Syntax: int strcmp(const char *str1, const char *str2);
    • Example:
				
					char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2); // result will be negative because "Hello" < "World"

				
			
  • strncmp()

    • Purpose: Compares up to n characters of two strings.
    • Syntax: int strncmp(const char *str1, const char *str2, size_t n);
    • Example:
				
					char str1[] = "Hello";
char str2[] = "Heaven";
int result = strncmp(str1, str2, 2); // result will be 0 because the first two characters are equal

				
			
  • strchr()

    • Purpose: Finds the first occurrence of a character in a string.
    • Syntax: char *strchr(const char *str, int c);
    • Example
				
					char str[] = "Hello, World!";
char *ptr = strchr(str, 'o'); // ptr points to the first 'o' in "Hello, World!"

				
			
  • strrchr()

    • Purpose: Finds the last occurrence of a character in a string.
    • Syntax: char *strrchr(const char *str, int c);
    • Example:
				
					char str[] = "Hello, World!";
char *ptr = strrchr(str, 'o'); // ptr points to the last 'o' in "Hello, World!"

				
			
  • strstr()

    • Purpose: Finds the first occurrence of a substring in a string.
    • Syntax: char *strstr(const char *haystack, const char *needle);
    • Example:
				
					char str[] = "Hello, World!";
char *ptr = strstr(str, "World"); // ptr points to the start of "World" in "Hello, World!"

				
			
  • strtok()

    • Purpose: Splits a string into tokens based on delimiters.
    • Syntax: char *strtok(char *str, const char *delim);
    • Example:
				
					char str[] = "Hello, World!";
char *token = strtok(str, ", "); // token will be "Hello"
while (token != NULL) {
    printf("%s\n", token);
    token = strtok(NULL, ", "); // next tokens will be processed
}

				
			

Example Program

Here’s a complete example program demonstrating the use of some of these functions:

				
					#include <stdio.h>
#include <string.h>

int main() {
    char str1[20] = "Hello";
    char str2[] = "World";

    // String length
    printf("Length of str1: %lu\n", strlen(str1));

    // String copy
    strcpy(str1, str2);
    printf("str1 after strcpy: %s\n", str1);

    // String concatenation
    strcat(str1, " Again");
    printf("str1 after strcat: %s\n", str1);

    // String comparison
    int cmp = strcmp(str1, str2);
    if (cmp == 0) {
        printf("str1 and str2 are equal\n");
    } else if (cmp < 0) {
        printf("str1 is less than str2\n");
    } else {
        printf("str1 is greater than str2\n");
    }

    // Find character in string
    char *ch = strchr(str1, 'W');
    if (ch != NULL) {
        printf("Character 'W' found at position: %ld\n", ch - str1);
    } else {
        printf("Character 'W' not found\n");
    }

    // Tokenizing string
    char str3[] = "Hello, World! Welcome to C programming.";
    char *token = strtok(str3, " ");
    while (token != NULL) {
        printf("Token: %s\n", token);
        token = strtok(NULL, " ");
    }

    return 0;
}

				
			

Conclusion

Strings in C are a fundamental concept, and understanding how to manipulate them using the standard library functions is essential for effective C programming. The functions provided by <string.h> allow for a wide range of operations, from simple copying and concatenation to more complex searching and tokenizing tasks.

Scroll to Top