Operations on Strings in C

Understanding Operations on Strings

String operations involve various manipulations such as concatenation, reversal, and comparison.

We will explore three different methods to perform operations on strings in C.

Method 1: Using Standard Library Functions

This method uses built-in string functions for operations.

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

int main() {
    char str1[100] = "Hello";
    char str2[] = " World";
    strcat(str1, str2);
    printf("Concatenated String: %s\n", str1);
    printf("Length of String: %ld\n", strlen(str1));
    return 0;
}
            
Output: Concatenated String: Hello World
Length of String: 11

Method 2: Using Character Array Manipulation

This method manually processes each character for operations.

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

void reverseString(char str[]) {
    int length = 0;
    while (str[length] != '\0') {
        length++;
    }
    for (int i = 0; i < length / 2; i++) {
        char temp = str[i];
        str[i] = str[length - i - 1];
        str[length - i - 1] = temp;
    }
}

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

Method 3: Using Pointer Manipulation

This method performs operations using pointers.

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

int compareStrings(char *str1, char *str2) {
    while (*str1 && *str2 && *str1 == *str2) {
        str1++;
        str2++;
    }
    return *str1 - *str2;
}

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

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Check whether a character is a vowel or consonant: C C++ Java Python

Check whether a character is an alphabet or not: C C++ Java Python

Find the ASCII value of a character: C C++ Java Python

Length of the string without using strlen() function: C C++ Java Python

Toggle each character in a string: C C++ Java Python

Count the number of vowels: C C++ Java Python

Remove the vowels from a string: C C++ Java Python

Check if the given string is Palindrome or not: C C++ Java Python

Print the given string in reverse order: C C++ Java Python

Remove all characters from string except alphabets: C C++ Java Python

Remove spaces from a string: C C++ Java Python

Replace a sub-string in a string: C C++ Java Python

Count common sub-sequences in two strings: C C++ Java Python

Compare two strings with wildcard support in one of them: C C++ Java Python

List all permutations of a given string in dictionary order: C C++ Java Python

Operations on Strings: C C++ Java Python