Sign in to follow this  

Beginning C programmer

This topic is 4201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am taking a beginning C programming class and have a question. I am trying to write a function that will check the user's input to see if the characters are vowel. Then I want to write the program to print out the user's input without the vowels. I have a block. The only thing I can think to do for the function is really long OR statement, if (c == 'a' || c == 'A' || c == 'e' || ... I know there must be an easier way. I asked a friend who is in C++, and he gave me a; switch(myString) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': return true; default: return false; } Can I use this in C? Or do I need another route?

Share this post


Link to post
Share on other sites
Give it a try, or check your learning reference. If I remember correctly, C supports fallthrough switches, though you might need to switch true/false to non-boolean values.

Share this post


Link to post
Share on other sites
C does support this construct, in the same way.

However, the usual idiom, in both languages, is to search for the character within a string that consists of all the characters you care about.

To do this in a bare-bones way (the only way you normally have in C unless you invent it yourself), you would make use of strchr(), with a char* string. In C++ (where you can do things in much nicer ways - but note that it's not just the language, it's the richness of the standard library which really helps you out), you can make use of the appropriate .find() member function of a std::string (or alternately, the free function std::find()).

Share this post


Link to post
Share on other sites
you can use somethink like this

void main()
{

char Vowels[10] = {'A','E','I','O','U','a','e','i','o','u'};

char MyVowel;
cout<<"Write a letter to see if it's a vowel\n";
cin >> MyVowel;
int IsVowel=0;
for (int x = 0;x !=10;x++)
{
if (MyVowel == Vowels[x]){IsVowel=1;}

}
if (IsVowel==1)cout<<"It's a vowel";
else cout<< "it isn't a vowel";


}

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Give it a try, or check your learning reference. If I remember correctly, C supports fallthrough switches, though you might need to switch true/false to non-boolean values.


Of course C supports fallthrough switches, you know how much a nightmare it would be to do stuff like this with if statements.

/* Returns information flags to determine if the tile is walkable, shootable, eg.. 
mostly though, this ia slot of slug work. :( */

unsigned char fTileConditions(unsigned int intTileX, unsigned int intTileY)
{
switch (arrMap[intTileY*intWidth+intTileX].bytTile)
{
/* These tiles are classified as Walkable */
default:
return flag_Walkable;
break;

/* These tiles are classified as ShootThrough */
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 16:
case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: case 31: case 32:
case 33: case 40: case 41: case 43: case 44: case 46: case 47: case 48: case 49: case 56:
case 58: case 59: case 60: case 61: case 62: case 63: case 64: case 71: case 72: case 73:
case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88: case 89: case 96:
case 97: case 98: case 99: case 100: case 101: case 102: case 103: case 104: case 108: case 109:
case 136: case 137: case 138: case 139: case 140: case 141: case 142: case 143: case 144: case 148:
case 149: case 150: case 162: case 188: case 189: case 194: case 195: case 111: case 112: case 113:
case 151: case 152: case 153: case 191: case 192: case 193: case 170: case 172: case 125: case 164:
case 166: case 163: case 202:
return flag_ShootThrough;
break;

/* These tiles are classified as Solid */
case 28: case 29: case 30: case 34: case 35: case 36: case 68: case 74: case 76: case 114:
case 115: case 116: case 154: case 156: case 176: case 177: case 178: case 179: case 180: case 181:
case 182: case 183: case 184: case 234: case 236: //case 0: case 0: case 0: case 0: case 0:
return flag_Solid;
break;
}
}

Share this post


Link to post
Share on other sites
There are two mistakes in your friend's code: He is comparing a string to char constants, and in C the bool values are constants and usually defined in all capital letters. But it will work fine in C after those changes are made.

Share this post


Link to post
Share on other sites

This topic is 4201 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this