No result illustrationNo result illustration

DevLoom

search

Lets Connect

chevron right
Menu
Home

Home

Community

Community

Blog Posts

Blog Posts

Programming

Programming

Tech Topics

Tech Topics

Explore

Explore

Find Jobs

Find Jobs

Tags

Tags

DevLoomPerfect Place for devs
    profile

    Jubair

    upvoteupvote

    0

    downvote

    0

    star

    How to Reverse Only Letters and Vowels in a String Using C++

    clock icon

    Asked 11 months ago

    message

    0Comments

    eye

    2 Views

    All the characters that are not English letters remain in the same position.

    All the English letters (lowercase or uppercase) should be reversed.

    Return s after reversing it.

    for example = d o g -g o d @ 4 3
    actual reverse = 3 4 @ d o g - g o d

    according to rule = 3 4 d - o g g @ o d

    Approch Two Pointer Approch

    d o g - g o d @ 4 3
    / /
    low high

    if low and high both are alphabets , then swap

    if s[high] is not alphabet high --
    if s[low] is not alphabet low ++

    Okay !

    how can we justify wheather a char is Alphabert

    a-z --> [97-122] ascii values
    A-Z -> [65-90] ascii valies

    if ((ch>=97 && ch<=122) || (ch>= 65 && ch<=90))
    {
    true ! its Alphabet
    }

    Alternative is using inbuild function : isalpha()
     
    using namespace std;
    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    string reverseOnlyLetters(string s)
    {
        int low = 0;
        int high = s.size() - 1;
    
        while (low < high)
        {
            if (isalpha(s[low]) && isalpha(s[high]))
            {
                swap(s[low], s[high]);
                low++, high--;
            }
            else if (!isalpha(s[low]))
            {
                low++;
            }
            else
            {
                high--;
            }
        }
        return s;
    };
    
    int main()
    {
    
        string s;
    
        cout << "Enter the String : " << endl;
        cin >> s;
    
        cout << "The Reverse Pattren is : " << reverseOnlyLetters(s) << endl;
    
        return 0;
    }
     

     Revesing only Vowels in a String

    
    // similar Problem ! reverse only letters
    
    // vowels = A E I O U // a e i o u
    
    // example Devloom
    
    // Dovloem
    
    using namespace std;
    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    bool isVowel(char ch)
    {
        ch = tolower(ch);
        return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
    }
    
    string reverseVowels(string s)
    {
        int low = 0;
        int high = s.size() - 1;
    
        while (low < high)
        {
            if (isVowel(s[low]) && isVowel(s[high]))
            {
                swap(s[low], s[high]);
                low++, high--;
            }
            else if (!isVowel(s[low]))
            {
                low++;
            }
            else
            {
                high--;
            }
        }
        return s;
    };
    
    int main()
    {
    
        string s;
    
        cout << "Enter the String : " << endl;
        cin >> s;
    
        cout << "The Reverse Vowels of a String is : " << reverseVowels(s) << endl;
    
        return 0;
    }
    Strings
    DSA
    cpp
    easy

    Share

    Write your comment here !

    0 Responses