Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Filtering Vowels and Formatting Consonants in Strings

Tech 1

The task requires processing an input string to remove all vowel characters (A, O, Y, E, U, I in both upper and lower case) and then format the remaining consonant characters. The formatting involves prefixing each consonant with a period ('.') and converting the character to its lowercase form. The algorithm must guarantee a non-empty output string.

The core algorithm is as follows:

  1. Define a set containing all target vowel characters, accounting for case sensitivity.
  2. Iterate through each character in the input string.
  3. For each character, check if it exists in the vowel set.
  4. If its a vowel, discard it.
  5. If it is a consonant, append a '.' character followed by the lowercase version of the consonant character to the result string.
  6. After processing all characters, the constructed result string is the final output.

The following C++ code implements this logic:

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main() {
    string inputStr, outputStr;
    // Read the input string
    getline(cin, inputStr);
    // Define vowel characters (upper and lower case)
    string vowels = "AaOoYyEeUuIi";
    // Process each character in the input
    for (char ch : inputStr) {
        // Check if the character is a vowel
        if (vowels.find(ch) != string::npos) {
            // Skip vowels
            continue;
        } else {
            // For consonants, add a dot and the lowercase character
            outputStr.push_back('.');
            outputStr.push_back(tolower(ch));
        }
    }
    // Output the final processed string
    cout << outputStr << endl;
    return 0;
}

Key components of the code:

  • getline(cin, inputStr): Reads a line of input, which can handle strings with spaces. cin >> inputStr would stop at whitespace.
  • string vowels: A string serving as a set of characters to identify vowels.
  • for (char ch : inputStr): A range-based for loop that iterates over each character ch in inputStr.
  • vowels.find(ch) != string::npos: Checks if the character ch exists within the vowels string. string::npos is a constant representing 'not found'.
  • outputStr.push_back('.'): Appends a period to the end of the result string.
  • outputStr.push_back(tolower(ch)): Appends the lowercase version of the consonant character. The tolower function from <cctype> handles the case conversion.
  • cout << outputStr << endl;: Prints the final formatted string.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.