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 <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main() {
    string str1 = "Hello";
    string str2 = " World";
    str1 += str2;
    cout << "Concatenated String: " << str1 << endl;
    cout << "Length of String: " << str1.length() << endl;
    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 <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

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

int main() {
    char str[] = "Hello";
    reverseString(str);
    cout << "Reversed String: " << str << endl;
    return 0;
}
            
Output: Reversed String: olleH

Method 3: Using Pointer Manipulation

This method performs operations using pointers.

#include <iostream>
#include <algorithm>
using namespace std;

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

int main() {
    const char str1[] = "Hello";
    const char str2[] = "Hello";
    int result = compareStrings(str1, str2);
    if (result == 0)
        cout << "Strings are equal" << endl;
    else
        cout << "Strings are not equal" << endl;
    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