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

    tcsRepeat using C++

    clock icon

    Asked 11 months ago

    message

    0Comments

    eye

    0 Views

    GIVEN PROBLEM STATEMENT ! 

    Given a string print YES if the string repeats after half by neglecting the case. For example,

    • Appleapple - o/p YES
    • okayPokay - o/p YES, (If odd number of terms are there, neglect the middle alphabet and then compute.)
    • madam - o/p NO
    • Pap - o/p YES

    Input Format

    A string (may even contain spaces)

    Constraints

    It may contain any valid characters (not just alphabets)

    Output Format

    YES or NO.

    
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int main() {
        string input;
        getline(cin, input);
        
        // Convert to lowercase
        transform(input.begin(), input.end(), input.begin(), ::tolower);
        
        // Remove middle character if odd length
        if (input.length() % 2 != 0) {
            input.erase(input.length() / 2, 1);
        }
        
        // Split the string
        int halfLength = input.length() / 2;
        string firstHalf = input.substr(0, halfLength);
        string secondHalf = input.substr(halfLength);
        
        // Compare and output result
        if (firstHalf == secondHalf) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
        
        return 0;
    }

    This solution follows these steps:

    1. Read the input string.
    2. Convert the string to lowercase to ignore case sensitivity.
    3. If the string length is odd, remove the middle character.
    4. Split the string into two halves.
    5. Compare the halves and output "YES" if they're identical, "NO" otherwise.

    This approach handles all the given cases:

    • "Appleapple" -> "YES"
    • "okayPokay" -> "YES"
    • "madam" -> "NO"
    • "Pap" -> "YES"
    • "GodogoD" -> "YES"

     

    TCS
    Strings
    medium

    Share

    Write your comment here !

    0 Responses