Looking for a simple

Dec 2, 2014 at 12:26am
My problem seems very simple - I have a string (a word), and I am supposed to copy all characters prior to the first vowel and paste them at the end of the word. For example, if I have the word "programming", I am supposed to make it "ogrammingpr". I tried using erase, append, insert functions, but I can't get any of them working. Any ideas?
Last edited on Dec 2, 2014 at 12:26am
Dec 2, 2014 at 12:32am
You'll probably use the find_first_of function to get the location of the first vowel. Then you can substr the word to get that bit, and put the two sections together.

Because I'm bored, here is an example (not tested):
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

std::string shiftAtVowel(const std::string& str) {
    std::size_t vowelPos = str.find_first_of("aeiouAEIOU");
    return str.substr(vowelPos) + str.substr(0, vowelPos);
}

// example use:
int main() {
    std::cout << shiftAtVowel("programming") << std::endl;
    return 0;
}
Dec 2, 2014 at 12:38am
run through all characters with a loop.
if the current character is no vowel, add it at the end: http://www.cplusplus.com/reference/string/string/operator+=/
if the current character is a vowel, remove all characters from the start until the current one: http://www.cplusplus.com/reference/string/string/erase/ and then exit the loop
Last edited on Dec 2, 2014 at 12:39am
Dec 2, 2014 at 1:01am
Merged a couple of ideas and worked.. thanks for the answers:)
Topic archived. No new replies allowed.