vendredi 9 avril 2021

Switching a's and b's in a String C++ [Code Wars Problem]

#include <string>
#include <algorithm> 
using namespace std;

std::string switcheroo(const std::string &s) {
  string myString = s;
  
  int i = 0;
  int j = s.length();
  
  if(s[i] != 'a' || s[i] != 'b') i++;
  if(s[j] != 'a' || s[j] != 'b') j--;
  else
  {
    swap(myString[i],myString[j]);
  }
  return myString;
}

Here is the link for the Code Kata problem in code wars https://www.codewars.com/kata/57f759bb664021a30300007d/train/cpp

It wants me to switch instances of a's and b's and I have done this problem before with switching vowels and so the same algorithm should work. What am I doing wrong with my code?

Aucun commentaire:

Enregistrer un commentaire