Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help sorting letters in strings

Status
Not open for further replies.

ednick

Programmer
May 17, 1999
4
0
0
US
Am writing program that will continue to prompt user to enter a word of 5 letters or less until 'q' is entered to quit. Upon quitting, it will disply results from 4 functions :<br>
1. total # of vowels (all words combined)<br>
2. total # of conenants (same as above)<br>
3. total # of words (simple counter)<br>
4. total # of words ending with 'k'<br>
Am using a while statement for the loop but can't get it to stop when {'q','\0'} is entered. Have assigned string to word[6] but it will take more than 5 letters and will not overwrite last word entered. <br>
All books I have referenced have sorting examples with numbers not letters and all examples have string or array within source code. <br>
Would appreciate any help that could be given.<br>
Thanks to all<br>
Ed
 
I think your main problem with failing to break out of the while loop is that you are checking for a lower case Q and not for an UPPERCASE Q..... You need to check for both. I prefer to use the Escape key for exiting because it is universally understood as an exit key by most users....<br>
Below is some code I reuse quite often, because it checks for extended key codes:<br>
<br>
int a;<br>
a = getch(); /* Get key press and Add 256 if extended key*/<br>
/* if your compiler doesn't have getch(), try getc() */<br>
if (a == 0 ¦¦ a == 0xE0) { a = getch() + 0x100; } <br>
switch ( a )<br>
{<br>
case 27: /* If escape key */<br>
printf( &quot;Exiting...&quot; );<br>
goto end;<br>
case 328: /* If Up Arrow key */<br>
break;<br>
case 336: /* If Down Arrow key */ <br>
break;<br>
default: /* Do other stuff */<br>
break;<br>
}<br>
<br>
You get the idea....<br>
If you put your words into a string ( a char array ) then your compiler's string functions should take care of the rest of the sorting functions for you with relative ease.<br>
Also, since you want to be able to count the # of characters inputted and stop at a certain point, it is better to use getch() and echo the characters to the display via your program, than fight with gets()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top