Connecting Tech Pros Worldwide Forums | Help | Site Map

need help to write some programs

Newbie
 
Join Date: Oct 2006
Posts: 19
#1: Oct 13 '06
plz, can you tell me how to write a c program (using functions)
i)to count the number of words in a sentence
ii)menu driven program to sort n names
iii)to sort the alphabets in a name,&to count the number of vowels, consonants,spaces&special characters in a sentence which is read from the user
advance thanx to those who help me

D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#2: Oct 13 '06

re: need help to write some programs


i) The number of words in a sentence is proportional to how many spaces you encounter. If it's single spaced, there may be one less space than word. If it's double spaced, it's probably 2* the number of words - 1. A finite state machine would easily describe how to do this. I assume there are no spaces after the last period, otherwise it may be off by one.
Expand|Select|Wrap|Line Numbers
  1. int count = 1;
  2. // string input = // some sentence
  3. // point a pointer at input
  4. while(the character pointed at by the pointer != '\0')
  5. {
  6.   while(contents of the pointer != ' ')
  7.     increment the pointer; // update it to next char
  8.   count++
  9.   while(contents of the pointer == ' ')
  10.      increment the pointer; // update it to next char
  11. }
ii) Do you know the value of N before you start entering the words to be sorted? In that case, you could use an array, of course link list also applies. In either case, I would use insertion sort.
Expand|Select|Wrap|Line Numbers
  1. input = // get next word
  2. cursor = first element in linked list/array
  3. while(input > cursor element)
  4.   cursor = next cursor;
  5. insert input, pushing the rest of the entries further down.
iii)Read in the name, sort it, then loop through each character, and if it qualifies as anything else, increment the counter. An example would have helped. Should "MacGyver" be "aceGMrvy" or "GMacervy"
Expand|Select|Wrap|Line Numbers
  1. // string input = "MacGyver" (for example)
  2. string sorted = "";
  3. int vowel_cnt = 0;
  4. int cnsnt_cnt = 0;
  5. int space_cnt = 0;
  6. int other_cnt = 0;
  7.  
  8. while(input.length() > 0)
  9. {
  10. // find the index of the character with the minimum value.
  11. // you may need to use pointer
  12. // a character array would be easier though.
  13.   sorted += // char with minimum value
  14.   if(char with min value is space)
  15.     space_cnt++;
  16.   else if('a' < the lowercase of char of min value < 'z') 
  17.   {
  18.     if(char with min value is a vowel)
  19.       vowel_cnt++;
  20.     else
  21.       cnsnt_cnt++;
  22.   }
  23.   else // assume special means not a space, vowel, or consonant
  24.     other_cnt++;
  25. }
  26.  
  27. // display string sorted, and the four counters.
  28. }
Reply