This question already has answers here:
How to replace all occurrences of a character in string?
(17 answers)
Closed 5 years ago.
How can I replace vowel letters with a character, for example input: asd output:.sd, so we replaced vowel letters with '.', I tried to do that, but I couldn't don't know why, here is my code:
#include <iostream>
using namespace std;
int main () {
string s;
cin>>s;
if (s=="a")
s='.';
if (s=="e")
s='.';
if (s=="u")
s='.';
if (s=="o")
s='.';
if (s=="i")
s='.';
cout<<s<<endl;
}
why nothing is changed? input = output?
You have a problem. When you read the string "s", it is equal to "asd". So when it is compared in the conditional (s=="a"), it is false. You must compare char by char. I share a possible solution:
#include <iostream>
int main()
{
std::string s;
std::cin >> s;
for (char& c: s)
{
if (c == 'a' || c=='e' || c == 'i' || c == 'o' || c == 'u')
c = '.';
}
std::cout << s << std::endl;
}
You can use std::replace_if to replace a character if it's a vowel, using std::string::find to easily figure out if the char is a vowel i.e. :
std::string s = "abcvaoi";
std::replace_if( s.begin( ), s.end( ), [] ( const char c ) {
return std::string( "aeiou" ).find( c ) != std::string::npos;
}, '.');
You may use switch case and for loop for simplicity.
using namespace std;
#include<iostream>
int main()
{
string a;
cin>>a;
for(int i=0;a[i]!='\0';i++)
{
switch (a[i])
{
case 'a':a[i]='.';
break;
case 'e':a[i]='.';
break;
case 'i':a[i]='.';
break;
case 'o':a[i]='.';
break;
case 'u':a[i]='.';
break;
}
}
cout<<a;
}
Related
I can't understand how I would do this.
The input will be:
3
13894
30-something
-Ex42
and the output needs to be:
13894
30
Ex42
The main assignment is to make a function that converts a duodecimal number into the decimal format. I have figured that part out and don't need help with it. I've basically cut out all the code surrounding the duodecimal conversion and just included the stuff I can't figure out.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int to_decimal(const string& str);
int main () {
string str; // Initializes string str for input
cin.ignore (256, '\n'); //ignores the 3 from the input
while (getline(cin, str)) {
//runs str through to_decimal and outputs
cout << to_decimal(str) << endl;
}
}
int to_decimal(const string& str) {
int f = 0;
string localString; // Initialize local string variable
//sets local string to the same as inputted string
localString = str; //used for local string erasing
//This is the idea I have been working on and I cant figure it out
for (unsigned x = 0; x < localString.length(); x++) {
f = localString.at(x);
if (isdigit(f)) {
} else if (f == 'E'){
} else if (f == 'e') {
} else if (f == 'X') {
} else if (f == 'x') {
} else if (f == '-') {
} else if (f == ' ') {
} else {
f = localString.length() - x;
localString.erase(x, f);
break;
}
}
}
I am a bit confused. You say that you need to convert duodecimal numbers to decimal, however in your sample output only the line that has Ex is converted, yet 30-something stays 30, as if it is not converted - and 30 in duodecimal is 36 in decimal. Same for the number 13894.
Assuming that you really want to convert all of the lines from duodecimal to decimal, you can base your solution on the standard library function std::stoi() which can convert a string from most number bases up to 36. It requires that the digits bigger than 9 are encoded using the letters in alphabetic order - A to Z. So you need to simply convert all you x to a and all you e to b. Example:
int to_decimal(const string& str) {
bool foundDigit = false;
std::string transformedString;
for (auto c : str) {
if (std::isdigit(c) || c == 'E' || c =='e' || c == 'X' || c == 'x') {
foundDigit = true;
// If needed, convert the character.
if (c == 'E' || c == 'e') {
c = 'b';
} else if (c == 'X' || c == 'x') {
c = 'a';
}
transformedString += c;
} else if (foundDigit) {
// Skip everything to the end of the line, if we've already found some digits
break;
}
}
return std::stoi(transformedString, 0, 12);
}
If you just want to extract the characters and then do the conversion yourself, then you can do something like this:
#include <iostream>
#include <string>
#include <sstream>
bool isNumber(const char c)
{
switch (c) {
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
case 'e':
case 'E':
case 'x':
case 'X':
case '-':
return true;
default:
return false;
}
}
std::string getNumber(std::istream& in)
{
std::stringstream s;
for (char c;in.get(c);)
{
if (isNumber(c)) {
s << c;
break;
}
}
for (char c;in.get(c);)
{
if (!isNumber(c))
break;
s << c;
}
return s.str();
}
int main()
{
std::string bla = "3\n13894\n 30-something\n-Ex42\n";
std::stringstream klaf{ bla };
for (std::string s;(s = getNumber(klaf)) != "";) //<- use a local stringstream as input to test
//for (std::string s;(s = getNumber(std::cin)) != "";) //<- use std::cin for input
{
std::cout << s << '\n';
}
}
This outputs:
3
13894
30-
e
-Ex42
So, not exactly what you were after, but it should at least get you a starting point to improve from. For example, you may want to remove - from isNumber and then change to logic in getNumber to only accept it as the first character in a new number.
This question already has answers here:
How to check if input is numeric in C++
(8 answers)
Closed 6 years ago.
I'm new to C++,
can I ask is there a way to check if a input string is a number?
If it is a number, change it to integer.
I know we can use either atoi or stoi.
But how can we create it in a function?
Construct a std::istringstream from the std::string.
Extract a number from it. If extraction is successful, use the number. If not, move on to the next thing.
std::string s = "123";
std::istringstream str(s);
int n;
if ( str >> n )
{
// Extraction is successful.
// Use n.
}
Further Enahncements
To make your program more robust, you can add a further check to make sure that:
"123FAST" is not treated as an integer.
"123.56" is not treated as an integer.
"123.56xyz" is not treated as an integer.
std::string s = "123";
std::istringstream str(s);
int n;
if ( str >> n )
{
// Extraction is successful.
// Add another check.
// Get the next character.
// It has to be a whitespace character or EOF for the input
// to be an integer.
// If it is neither, don't accept the input as an integer.
std::istream::int_type c = str.get();
if ( std::isspace(c) || str.eof() )
{
// Use n.
}
}
You just check each character in your input string is a number, I have a simple example for your problem:
#include <stdio.h>
#include <string.h>
bool is_number(const char * s)
{
int length = strlen(s);
for (int i = 0; i < length; i++)
{
if (s[i] < '0' || s[i] > '9')
return false;
}
return true;
}
int main()
{
printf("Enter a string: ");
char myString[10];
gets(myString);
if (is_number(myString))
printf("%s is a number", myString);
else
printf("%s is not a number", myString);
}
I don't think you even need to check. According to the C++ API:
If the first sequence of non-whitespace characters in str is not a
valid integral number, or if no such sequence exists because either
str is empty or it contains only whitespace characters, no conversion
is performed and zero is returned.
SEE:
http://www.cplusplus.com/reference/cstdlib/atoi/
-
So, you should just be able to do:
// Convert the string
int value = atoi( myString.c_str() );
if ( value == 0 )
{
// The string was not a valid integer
}
else
{
// The string was a valid integer
}
'isnum()' function below checks if given string is comprised of numbers only. However it might not work if number is very large.
#include <bits/stdc++.h>
using namespace std;
bool isnum(string str){
for(int i=0;i<str.length();i++){
if((str[i]<'0') || (str[i]>'9'))return false;
}
return true;
}
int main() {
// your code goes here
int n=0,r=1;
string num;
cin>>num;
if(isnum(num)){
for(int i=num.length()-1;i>=0;i--){
n+=(r)*(int)(num[i]-'0');
r*=10;
}
cout<<n;
}
return 0;
}
You can check if there is any non-digit character in the string:
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
int main()
{
std::string test;
std::cin >> test;
if(std::find_if(test.begin(), test.end(), [](auto x){return !isdigit(x);}) == test.end())
std::cout << "its a number" << std::endl;
else
std::cout << "not a number" << std::endl;
}
I'm having trouble implementing a feature that counts and displays the number of vowels from a file.
Here is the code I have so far.
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstdio>
using namespace std;
int main(void)
{int i;
string inputFileName;
string s;
ifstream fileIn;
char ch;
cout<<"Enter name of file of characters :";
cin>>inputFileName;
fileIn.open(inputFileName.data());
assert(fileIn.is_open() );
i=0;
while (!(fileIn.eof()))
{
????????????
}
cout<<s;
cout<<"The number of vowels in the string is "<<s.?()<<endl;
return 0;
}
Note the question marks in the code.
Questions: How should I go about counting the vowels? Do I have to convert the text to lowercase and invoke system controls (if possible)?
Also, as for printing the number of vowels in the end, which string variable should I use, (see s.?)?
Thanks
auto isvowel = [](char c){ return c == 'A' || c == 'a' ||
c == 'E' || c == 'e' ||
c == 'I' || c == 'i' ||
c == 'O' || c == 'o' ||
c == 'U' || c == 'u'; };
std::ifstream f("file.txt");
auto numVowels = std::count_if(std::istreambuf_iterator<char>(f),
std::istreambuf_iterator<char>(),
isvowel);
You can using <algorithm>'s std::count_if to achieve this :
std::string vowels = "AEIOUaeiou";
size_t count = std::count_if
(
std::istreambuf_iterator<char>(in),
std::istreambuf_iterator<char>(),
[=]( char x)
{
return vowels.find(x) != std::string::npos ;
}
);
Or
size_t count = 0;
std::string vowels = "AEIOUaeiou";
char x ;
while ( in >> x )
{
count += vowels.find(x) != std::string::npos ;
}
Also read Why is iostream::eof inside a loop condition considered wrong?
Write a c++ program that finds the number of vowels used in an string.
For the above problem I written a program as follows:
int main()
{
char x[10];
int n,i,s=0;
cout<<"Enter any string\n";
cin>>x;
n=strlen(x);
for(i=0;i<n;++i)
{
if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u')
{
s=s+1;
}
}
cout<<s;
return 0;
}
Output of the program is as:
Enter any string elephant 3
Here in 'elephant' at three places vowels are used but the total number of vowels used is 2(e and a) not 3
I am asking to improve the program so that it counts the total number of vowels and print the total number.(e.g. in case of elephant it must give 2)
Make another array(), with 5 index, like
vowels[5] = array(0,0,0,0,0);
Then make if else if, with eache vowel, and add
if(x[i] == 'a') vowels[0] =1;
elseIf(x[i] == 'e') vowels[1] =1;
etc, and then check if vowels array is set to 1 or 0, and count only, these which are 5.
int count=0;
foreach vowels as item {
if(item == 1) count++
}
return count;
The easiest solution would be to just insert each vowel you see
into an std::set, and use its size function when you're
done.
And for heaven's sake, use a table lookup to determine whether
something is a vowel (and put the logic in a separate function,
so you can correct it when you need to handle the "sometimes y"
part).
Alternatively, without using the standard algorithms:
int charCount[UCHAR_MAX + 1];
// and for each character:
++ charCount[static_cast<unsigned char>( ch )];
(Of course, if you're using C++, you'll read the characters
into an std::string, and iterate over that, rather than having
an almost guaranteed buffer overflow.)
Then, just look at each of the vowels in the table, and count
those which have non-zero counts:
int results = 0;
std::string vowels( "aeiou" ); // Handling the sometimes "y" is left as an exercise for the reader.
for ( auto current = vowels.begin(); current != vowels.end(); ++ current ) {
if ( charCount[static_cast<unsigned char>( *current )] != 0 ) {
++ results;
}
}
Of course, neither of these, implemented naïvely, will handle
upper and lower case correctly (where 'E' and 'e' are the same
vowel); using tolower( static_cast<unsigned char>( ch ) ) will
solve that.
EDIT:
Since others are proposing solutions (which are only partially
correct):
bool
isVowel( unsigned char ch )
{
static std::set<int> const vowels{ 'a', 'e', 'i', 'o', 'u' };
return vowels.find( tolower( ch ) ) != vowels.end();
}
int
main()
{
std::string text;
std::cout << "Enter any word:";
std::cin >> text;
std::set<unsigned char> vowelsPresent;
for ( unsigned char ch: text ) {
if ( isVowel( ch ) ) {
vowelsPresent.insert( tolower( ch ) );
}
}
std::cout << vowelsPresent.size() << std::endl;
}
Separating the definition of a vowel into a separate function is
practically essential in well written code, and at the very
least, you need to mask differences in case. (This code also
punts on the question of "y", which would make isVowel several
orders of magnitude more difficult. It also ignores characters
outside of the basic character set, so "naïve" will report two
different vowels.)
Sets already eliminate duplicates, so instead of counting vowels as you encounter them, add them into a set. Then, at the end, count the number of [non-duplicate] vowels by querying the set for its size.
#include <set>
#include <string>
#include <iostream>
int main()
{
std::string x;
int n = 0;
std::set<char> vowels;
std::cout << "Enter any string\n";
std::cin >> x;
n = x.size();
for (int i = 0; i < n; ++i)
if (x[i] == 'a' || x[i] == 'e' || x[i] == 'i' || x[i] == 'o' || x[i] == 'u')
vowels.insert(x[i]);
std::cout << vowels.size() <<'\n';
}
Live demo
g++-4.8 -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && echo "elephant" | ./a.out
Enter any string
2
Note that I also exchanged your use of fixed-sized arrays with an std::string, so that you're not at risk of dangerous circumstances when someone happens to input more than 9 characters.
I find a really easy way to solve this problem is by using map <char, int>. This will allow you to make pairs, indexed by a char, ie. the vowels, and connect an integer counter to them.
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map <char, int> vowels;
int n,i,s=0;
string x;
cout<<"Enter any string\n";
cin>>x;
for(i=0;i< x.length();++i)
{
if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u')
{
vowels[x[i]]++;
}
}
for (map<char,int>::const_iterator print = vowels.begin(); print != vowels.end(); ++print){
cout << print -> first << " " << print -> second << endl;
}
return 0;
}
For the string elephant we would get the following output:
a 1
e 2
By saying vowels[x[i]]++; we are adding the found vowel into our map, if it already has not been added, and incrementing its paired int by one. So when we find the first e it will add e to our map and increment its counter by one. Then it will continue until it finds the next e and will see that it already has that indexed, so it will simply increment the counter to 2. This way we will avoid the problem with duplicates. Of course, if you wanted to get a single digit we could just print out the size of our map:
cout << vowels.size() << endl;
Okay. My turn. To handle both upper and lower cases we convert to just lower:
std::string x("Elephant");
std::transform(x.begin(), x.end(), x.begin(), std::function<int(char)>(std::tolower));
Now remove duplicates:
std::sort(x.begin(), x.end());
std::unique(x.begin(), x.end());
Now to count the vowels. I was hoping for something specific in locale but alas... Never mind we can create our own. Bit more complex, but not overly:
struct vowel : public std::ctype<char>
{
static const mask* make_table()
{
static std::vector<mask> v(classic_table(), classic_table() + table_size);
v['a'] |= upper;
v['e'] |= upper;
// etc.
return &v[0];
}
vowel(std::size_t refs = 0) : ctype(make_table(), false, refs){}
};
While I am sure you can create your own but can't quite figure out how going by the documentation on cppreference so I say lower case vowels are uppercase. With the earlier call to std::tolower this should be safe.
With this we can use it easily like:
int i = std::count_if(x.begin(), x.end(), [](const char c)
{
return std::isupper(c, std::locale(std::locale(""), new vowel));
});
std::cout << "Number of vowels:" << i << std::endl;
However I am not particularly happy with the two std::locale next each other.
Easiest solution I can think of would be an array of bools representing each vowel and whether or not they've been counted.
bool vowelCounted[5] = { false };
Now, as you count the vowels:
if (x[i]=='a' && !vowelCounted[0]) {
vowelCounted[0] = true;
s += 1;
} else if (x[i]=='e' && !vowelCounted[1]) {
vowelCounted[1] = true;
s += 1;
}
And just repeat this structure for all 5 vowels.
The readability can be improved by using an enum rather than 0, 1, 2, 3, 4 for your indices... but you're using variables named x[] and s, so it's probably fine...
If you use one of the standard containers (vector, list) add your vowels in there, do the same check as you're doing now, if it exists then remove it. When you're finished get the number of remaining elements, your answer will be the original count for the vowels minus the the remaining elements.
try this
for( string text; getline( cin, text ) && text != "q"; )
{
set< char > vowels;
copy_if( begin(text), end(text), inserter( vowels, begin(vowels) ),
[]( char c ) { return std::char_traits< char >::find( "aeiou", 5, c ) != nullptr; } );
cout << "the string [" << text << "] contains " << vowels.size() << " vowels" << endl;
}
You need the includes string, iostream, set, algorithm and iterator.
What do You want to do with the upper ones "AEIOU" ?
You can create this:
std::vector< char> vowels;
And put into it all vowels that you meet while iterating through the string:
if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u')
vowels.push_back( x[i]);
Then you can sort this and eliminate duplicates:
std::sort( vowels.begin(), vowels.end());
std::vector< char> vowels_unique( vowels.size());
std::vector< char>::iterator it;
it = std::unique_copy( vowels.begin(), vowels.end(), vowels_unique.begin());
vowels_unique.resize( std::distance( vowels_unique.begin(), it));
Even better, use a set property - it holds unique data, like this:
std::set< char> unique_vowels;
if(x[i]=='a'||x[i]=='e'||x[i]=='i'||x[i]=='o'||x[i]=='u')
unique_vowels.insert( x[i]);
//...
int unique = unique_vowels.size();
C++ Code Snippet :
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s[100];
int cnt;
cnt=0;
cin>>s;
for(int i=0;s[i];i++)
{
char c =s[i];
if(s[i]=='A' || s[i] =='E' || s[i]=='I' ||s[i]=='O'|| s[i]=='U') cnt++;
else if(s[i]=='a' || s[i] =='e' || s[i]=='i'||s[i]=='o' || s[i]=='u') cnt++;
}
cout<<cnt<<endl;
return 0;
}
Version utilizing std::find() and std::transform():
#include <string>
#include <iostream>
#include <algorithm>
using std::string;
using std::cout;
using std::cin;
using std::getline;
using std::transform;
int main()
{
cout << " Type sentence: ";
string sentence;
getline(cin, sentence);
transform(sentence.begin(), sentence.end(), sentence.begin(), toupper);
string vowels = "AEIOU";
size_t vowCount = 0;
for (int i = 0; i < vowels.length(); ++i)
{
if (sentence.find(vowels[i], 0) != string::npos)
{
++vowCount;
}
}
cout << "There is " << vowCount << " vowels in the sentence.\n";
return 0;
}
PROS
std::find() searches just for first occurrence of given vowel so there is no iteration over rest of the given string
using optimized algorithms from std
CONS
std::transform() transforms each lower-case letter regardless of its "vowelness"
#include<iostream> //std::cout
#include<string> //std::string
#include<cctype> //tolower()
#include<algorithm> //std::for_each
using namespace std;
int main()
{
string s;
getline(cin, s);
int count = 0;
for_each(s.begin(), s.end(), [&count](char & c) //use of lambda func
{
c = tolower(c); //you can count upper and lower vowels
switch (c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
count++;
break;
}
});
cout << count << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
char vowels[5] = {'a','e','i','o','u'};
char x[8] = {'e','l','e','p','h','a','n','t'};
int counts[5] = {0,0,0,0,0};
int i,j;
for(i=0;i<8;i=i+1)
{
for(j=0;j<5;j=j+1)
{
if(x[i]==vowels[j])
{
counts[j] = counts[j] + 1;
}
}
}
for(i=0;i<5;i=i+1)
{
cout<<counts[i]<<endl;
}
return 0;
}
Since I was using the example of 'elephant' so I just initialized that.
If still any modification can be made then please edit it and make it more user friendly.
I want to add '.' character besides another character in a string but I don't know how to do it ? is it possible?
#include <iostream>
#include <string.h>
using namespace std;
int main(int argc, char *argv[]) {
string input;
char dot='.';
cin>>input;
for(int i=0;i<input.length();i++)
{
if( input[i]>=65 && input[i]<=90)
{
input[i]=input[i]+32;
}
if( (input[i]=='a') || (input[i]=='e') || (input[i]=='i') || (input[i]=='o') || input[i]=='y' || input[i]=='u' )
{
input.erase(i,i+1);
}
input[i]+=dot;
}
cout<<input<<endl;
return 0;
}
From the cpluplus.com reference ( http://www.cplusplus.com/reference/string/string/insert/ )
// inserting into a string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str="to be question";
string str2="the ";
string str3="or not to be";
string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
cout << str << endl;
return 0;
}
Also, check these links:
http://www.cplusplus.com/reference/string/string/
http://www.cplusplus.com/reference/string/string/append/
http://www.cplusplus.com/reference/string/string/push_back/
Before you try writing the code, you should write a detailed
specification of what it is supposed to do. With your code, I
can only guess: convert to lower case (naïvely, pretending that
you'll only encounter the 26 unaccented letters in ASCII), then
delete all vowels (again, very naïvely, since determining
whether something is a vowel or not is non-trivial, even in
English—consider the y in yet and day), and finally
inserting a dot after each character. The most obvious way of
doing that would be something like:
std::string results;
for ( std::string::const_iterator current = input.begin(),
end = input.end();
current != end;
++ current ) {
static std::string const vowels( "aeiouAEIOU" );
if ( std::find( vowels.begin(), vowels.end(), *current )
!= vowels.end() ) {
results.push_back(
tolower( static_cast<unsigned char>( *current ) ) );
}
results.push_back( '.' );
}
But again, I'm just guessing as to what you are trying to do.
Another alternative would be to use std::transform on the
initial string to make it all lower case. If you're doing this
sort of thing regularly, you'll have a ToLower functional
object; otherwise, it's probably too much of a bother to write
one just to be able to use std::transform once.
I’m assuming you want this input:
Hello world!
To give you this output:
h.ll. w.rld!
Rather than trying to modify the string in place, you can simply produce a new string as you go:
#include <cctype>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
string input;
getline(cin, input);
string output;
const string vowels = "aeiouy";
for (int i = 0; i < input.size(); ++i) {
const char c = tolower(input[i]);
if (vowels.find(c) != string::npos) {
output += '.';
} else {
output += c;
}
}
cout << output << '\n';
return 0;
}
Notes:
<cctype> is for toupper().
<string.h> is deprecated; use <string>.
Read whole lines with getline(); istream::operator>>() reads words.
Use tolower(), toupper(), &c. for character transformations. c + 32 doesn’t describe your intent.
When you need comparisons, c >= 'A' && c <= 'Z' will work; you don't need to use ASCII codes.
Use const for things that will not change.
I'm not sure how this old question got bumped back onto the current list, but after reviewing the answers, it looks like all will miss the mark if the input is more than a single word. From your comments, it appears you want to remove all vowels and place a '.' before the character immediately prior to where the removal occurred. Thus your example "tour" becomes ".t.r".
Drawing from the other answers, and shamelessly removing 'y' as from the list of vowels, you can do something similar to:
#include <iostream>
#include <string>
int main()
{
std::string input;
if (!getline (std::cin, input)) {
return 1;
}
size_t i = 0;
for (; input[i]; i++)
{
switch (input[i])
{
case 'A': /* case fall-through intentional */
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
{
size_t pos = input.find_first_not_of("AEIOUaeiou", i+1);
if (pos == std::string::npos) {
pos = input.length();
}
input.erase(i, pos-i);
if (pos - i > 1) {
input.insert(i, 1, '.');
}
input.insert(i-1, 1, '.');
break;
}
}
}
std::cout << input << '\n';
}
Example Use/Output
Your example:
$ ./bin/vowels-rm-mark
tour
.t.r
A longer example:
$ ./bin/vowels-rm-mark
My dog has fleas and my cat has none.
My .dg .hs f.l.s. nd my .ct .hs .n.n.
Based on your comments, it sounds like you want something like this:
#include <iostream>
#include <string>
#include <algorithm>
int main(int argc, char *argv[])
{
std::string input;
std::cin >> input;
std::transform (input.begin(), input.end(), input.begin(), tolower);
size_t i = 0;
while (i < input.length())
{
switch (input[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'y':
case 'u':
{
size_t pos = input.find_first_not_of("aeioyu", i+1);
if (pos == std::string::npos)
pos = input.length();
input.erase(i, pos-i);
break;
}
default:
{
input.insert(i, 1, '.'); // or: input.insert(i, ".");
i += 2;
break;
}
}
}
std::cout << input << std::endl;
return 0;
}