need help to write some programs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sowmyth
    New Member
    • Oct 2006
    • 19

    need help to write some programs

    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,spac es&special characters in a sentence which is read from the user
    advance thanx to those who help me
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    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.
    Code:
    int count = 1;
    // string input = // some sentence
    // point a pointer at input
    while(the character pointed at by the pointer != '\0')
    {
      while(contents of the pointer != ' ')
        increment the pointer; // update it to next char
      count++
      while(contents of the pointer == ' ')
         increment the pointer; // update it to next char
    }
    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.
    Code:
    input = // get next word
    cursor = first element in linked list/array
    while(input > cursor element)
      cursor = next cursor;
    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"
    Code:
    // string input = "MacGyver" (for example)
    string sorted = "";
    int vowel_cnt = 0;
    int cnsnt_cnt = 0;
    int space_cnt = 0;
    int other_cnt = 0;
    
    while(input.length() > 0)
    {
    // find the index of the character with the minimum value.
    // you may need to use pointer
    // a character array would be easier though.
      sorted += // char with minimum value
      if(char with min value is space)
        space_cnt++;
      else if('a' < the lowercase of char of min value < 'z') 
      {
        if(char with min value is a vowel)
          vowel_cnt++;
        else
          cnsnt_cnt++;
      }
      else // assume special means not a space, vowel, or consonant
        other_cnt++;
    }
    
    // display string sorted, and the four counters.
    }

    Comment

    Working...