samedi 26 novembre 2016

shuffle letters in a string c++

I'm meant to write a function that keeps the first and last letter of a string the same and ignores any non-letters after the last letter. I'm supposed to use the std::random_shuffle(). I read the documentation however I don't seem to grasp the concept of this function. This is my code:

#include <iostream>
#include <algorithm>
#include <string>

std::string mix(std::string s){
    int last_char;
    if(std::isalpha(s[s.size()-1]) == true){
        last_char = s.size()-1;
    } else {
     for(int i=s.size() ; i>0; --i){
        if((std::isalpha(s[i]) == false) && (std::isalpha(s[i-1])==true)){
            last_char = i -1;
            break;
        }
      }
    }
    std::random_shuffle(s[1],s[last_char-1]);

}

int main(){
    std::string test = "Hello";
    std::cout << mix(test) << std::endl;
}




Aucun commentaire:

Enregistrer un commentaire