In this section, we will explore how to handle strings in C. Strings are a crucial part of many programs, and understanding how to manipulate them is essential for any C programmer. We will cover the following topics:

  1. Introduction to Strings
  2. String Initialization and Declaration
  3. Common String Functions
  4. String Manipulation Examples
  5. Practical Exercises

  1. Introduction to Strings

In C, a string is essentially an array of characters terminated by a null character ('\0'). This null character indicates the end of the string.

Key Points:

  • Strings are arrays of characters.
  • The last character of a string is always '\0'.
  • Strings can be manipulated using various standard library functions.

  1. String Initialization and Declaration

Declaring a String:

You can declare a string in C in several ways:

char str1[10]; // Declares a string with a maximum length of 9 characters (plus the null character)
char str2[] = "Hello"; // Declares and initializes a string
char str3[6] = "World"; // Declares and initializes a string with a specific size

Initializing a String:

You can initialize a string at the time of declaration:

char str1[] = "Hello, World!";
char str2[20] = "C Programming";

Important Note:

When initializing a string, ensure that the array size is large enough to hold the string and the null character.

  1. Common String Functions

C provides several standard library functions to manipulate strings. These functions are declared in the <string.h> header file.

Common Functions:

Function Description
strlen Returns the length of the string (excluding the null character).
strcpy Copies one string to another.
strcat Concatenates (appends) one string to another.
strcmp Compares two strings.
strchr Finds the first occurrence of a character in a string.
strstr Finds the first occurrence of a substring in a string.

Examples:

strlen Example:

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

int main() {
    char str[] = "Hello, World!";
    printf("Length of the string: %lu\n", strlen(str));
    return 0;
}

strcpy Example:

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

int main() {
    char src[] = "Hello";
    char dest[10];
    strcpy(dest, src);
    printf("Destination string: %s\n", dest);
    return 0;
}

strcat Example:

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

int main() {
    char str1[20] = "Hello";
    char str2[] = ", World!";
    strcat(str1, str2);
    printf("Concatenated string: %s\n", str1);
    return 0;
}

strcmp Example:

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

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
    int result = strcmp(str1, str2);
    if (result == 0) {
        printf("Strings are equal\n");
    } else {
        printf("Strings are not equal\n");
    }
    return 0;
}

  1. String Manipulation Examples

Example 1: Reversing a String

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

void reverseString(char str[]) {
    int n = strlen(str);
    for (int i = 0; i < n / 2; i++) {
        char temp = str[i];
        str[i] = str[n - i - 1];
        str[n - i - 1] = temp;
    }
}

int main() {
    char str[] = "Hello, World!";
    reverseString(str);
    printf("Reversed string: %s\n", str);
    return 0;
}

Example 2: Counting Vowels in a String

#include <stdio.h>

int countVowels(char str[]) {
    int count = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        char c = str[i];
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
            c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
            count++;
        }
    }
    return count;
}

int main() {
    char str[] = "Hello, World!";
    int vowels = countVowels(str);
    printf("Number of vowels: %d\n", vowels);
    return 0;
}

  1. Practical Exercises

Exercise 1: Palindrome Checker

Write a program to check if a given string is a palindrome (reads the same forwards and backwards).

Solution:

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

bool isPalindrome(char str[]) {
    int n = strlen(str);
    for (int i = 0; i < n / 2; i++) {
        if (str[i] != str[n - i - 1]) {
            return false;
        }
    }
    return true;
}

int main() {
    char str[] = "madam";
    if (isPalindrome(str)) {
        printf("The string is a palindrome.\n");
    } else {
        printf("The string is not a palindrome.\n");
    }
    return 0;
}

Exercise 2: String to Uppercase

Write a program to convert a given string to uppercase.

Solution:

#include <stdio.h>
#include <ctype.h>

void toUpperCase(char str[]) {
    for (int i = 0; str[i] != '\0'; i++) {
        str[i] = toupper(str[i]);
    }
}

int main() {
    char str[] = "Hello, World!";
    toUpperCase(str);
    printf("Uppercase string: %s\n", str);
    return 0;
}

Conclusion

In this section, we covered the basics of string handling in C, including string initialization, common string functions, and practical examples. Understanding these concepts is crucial for manipulating text data in your programs. In the next section, we will delve into arrays and their applications in C programming.

© Copyright 2024. All rights reserved