Feb 2, 2012 at 1:56am UTC
Hi
I know this is the simplest thing ever but everything I tried failed surprisingly :/
Basically i want an input that is "blue car". And I want a condition to be true when its both "blue car" , "bluecar" , "Blue car" and "Blue Car".
That space character is really bugging me.
I'm pretty sure its possible but i cant find a way :/
Thanks for your time
Feb 2, 2012 at 3:00am UTC
Yea I tried that but it only works when the input is bluecar.. I feel like I am doing something wrong could you please check this?
#include <iostream>
#include <cstdio>
using namespace std;
main(){
string input;
cin >> input;
if (input == "blue car" || input == "bluecar" || input == "Blue car" || input == "Blue car")
{
cout << "Success\n";
}
else
{
cout << "Fail\n";
}
system ("pause");
}
Feb 2, 2012 at 3:23am UTC
Well you could write a simple function that removes all spaces and forces every character to a lower case. I have gone ahead an written the function for you, feel free to use it. This is a pretty sloppy way of going about this but I only spent a few minutes on it so feel free to make whatever changes you like.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
void SimplifyString( std::string &Original )
{
int Length = Original.length();
for ( int i = 0; i < Length; i ++ )
{
if ( isspace( Original.at( i ) ) )
{
Original.erase( i, 1 );
i = 0;
Length = Original.length();
}
Original.at( i ) = tolower( Original.at( i ) );
}
}
void main()
{
std::string Test = "TEST STRring 1233\t\tblah" ;
SimplifyString( Test );
std::cout << Test; // Output: teststring1233blah
}
Last edited on Feb 2, 2012 at 3:49am UTC
Feb 2, 2012 at 3:24am UTC
On my compiler, VC++ express 10, only "blue" gets stored in input when the input includes a space.
Might want to use getline and assign the input to a string.
Last edited on Feb 2, 2012 at 3:31am UTC
Feb 2, 2012 at 3:45am UTC
Wow that works codingshark thank you very much, kudos.