Practical String Manipulation Problems in C++

Problem A: String Concatenation

Solution

#include <iostream>
#include <string>

int main() {
    std::string str1, str2;
    while (std::cin >> str1 >> str2) {
        str1.append(str2);
        std::cout << str1 << std::endl;
    }
    return 0;
}

Problem C: Case-Insensitive Substring Deletion

Analysis

Key requirements:

  1. Deletion must be case-insensitive, but the original string must be preserved for output
  2. The pattern string contains no spaces, so scanf("%s") works for it
  3. Target strings may contain spaces, requiring gets() for input
  4. When mixing scanf("%s") with gets(), use getchar() to consume the trailing newline

Approach 1: Character-by-character comparison

#include <cstdio>
#include <cstring>
#include <cmath>

int main() {
    char pattern[20], text[200];
    int patLen, textLen;
    bool matched;
    
    scanf("%s", pattern);
    patLen = strlen(pattern);
    getchar();
    
    while (gets(text) != NULL) {
        textLen = strlen(text);
        for (int i = 0; i < textLen; i++) {
            // Case-insensitive character comparison
            if (fabs(text[i] - pattern[0]) == 32 || text[i] - pattern[0] == 0) {
                matched = true;
                for (int j = 1; j < patLen; j++) {
                    if (fabs(text[i + j] - pattern[j]) != 32 && text[i + j] - pattern[j] != 0) {
                        matched = false;
                        break;
                    }
                }
                if (matched) {
                    i += patLen - 1;
                } else if (text[i] != ' ') {
                    printf("%c", text[i]);
                }
            } else if (text[i] != ' ') {
                printf("%c", text[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

Approach 2: Using string library functions

#include <iostream>
#include <string>
#include <cctype>

int main() {
    char buffer[200];
    std::string pattern, original, working;
    
    std::cin >> pattern;
    getchar();
    
    for (int i = 0; i < pattern.length(); i++) {
        pattern[i] = std::tolower(pattern[i]);
    }
    
    while (gets(buffer) != NULL) {
        original = buffer;
        working = buffer;
        
        for (int i = 0; i < working.length(); i++) {
            working[i] = std::tolower(working[i]);
        }
        
        size_t pos = working.find(pattern);
        while (pos != std::string::npos) {
            original.erase(pos, pattern.length());
            working.erase(pos, pattern.length());
            pos = working.find(pattern);
        }
        
        size_t spacePos = working.find(' ');
        while (spacePos != std::string::npos) {
            original.erase(spacePos, 1);
            working.erase(spacePos, 1);
            spacePos = working.find(' ');
        }
        
        std::cout << original << std::endl;
    }
    return 0;
}

Problem D: Word Replacement

Analysis

This problem is similar to the previous one. Since the presence of spaces in the input strings is not specified, use gets() for all input.

Solution

#include <cstdio>
#include <cstring>

int main() {
    char source[105], search[105], replacement[105];
    bool matched;
    
    while (gets(source) != NULL) {
        gets(search);
        gets(replacement);
        
        int srcLen = strlen(source);
        int searchLen = strlen(search);
        int replLen = strlen(replacement);
        
        for (int i = 0; i < srcLen; i++) {
            matched = true;
            if (source[i] == search[0]) {
                for (int j = 1; j < searchLen; j++) {
                    if (source[i + j] != search[j]) {
                        matched = false;
                        break;
                    }
                }
                if (matched) {
                    for (int k = 0; k < replLen; k++) {
                        printf("%c", replacement[k]);
                    }
                    i += searchLen - 1;
                } else {
                    printf("%c", source[i]);
                }
            } else {
                printf("%c", source[i]);
            }
        }
        printf("\n");
    }
    return 0;
}

Problem E: Remove Specific Characters

Analysis

The problem requires continuous input. The character to remove is read using getchar(). To prevent leftover characters (like newlines) from interfering with subsequent gets() calls, consume any remaining characters in the input buffer at the end of each loop iteration.

Solution

#include <cstdio>
#include <cstring>

int main() {
    char text[1000];
    char targetChar;
    int ch;
    
    while (gets(text)) {
        targetChar = getchar();
        
        for (int i = 0; i < strlen(text); i++) {
            if (text[i] != targetChar) {
                printf("%c", text[i]);
            }
        }
        printf("\n");
        
        // Discard remaining characters in input buffer
        while ((ch = getchar()) != '\n' && ch != EOF);
    }
    return 0;
}

Tags: C++ String Manipulation character processing Competitive Programming

Posted on Thu, 15 Oct 2026 16:44:26 +0000 by hcspider