String

Pages: 12
I know it's not the best way of learning but I'm trying my level best to understand c++ without additional help from a teacher.
Honestly, there are many textbooks that can do the job of a teacher just fine.

I'm not sure how it's going to help you, but here is the code anyway:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>

std::string remove_repeated_letters(std::string word)
{
   const char* w = word.c_str();
   std::string result = {};
   
   for (int count = 1; count <= word.size(); count ++)
   {
       if (w[count] != w[count - 1])
       {
           result += w[count - 1];
       }
   }
   
   return result;
}

int main ( ) 
{
    std::string input;
    
    std::cout << " Enter word to test : ";
    std::cin >> input;
    
    std::string result = remove_repeated_letters(input);
    std::cout << " Your word without repeated letters is : " << result
        << "\n Is your word \"banana\"?" << "\tThe answer is : ";
    
    if (result == "banana") std::cout << "Yes";
    else std::cout << "No";        
}


Hope this helps...
Topic archived. No new replies allowed.
Pages: 12