Thread: Ever just need a second set of eyes to help?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Ever just need a second set of eyes to help?

    For some reason I decided to go back to school to get my degree in this stuff... I am taking a programming in c class and have been assigned a task to 1) load a string with user promt, 2) allow user to select to count the occurence of each letter in the string regardless if upper or lower case... or select to reverse the word order from end to finish... words separated by tab or space.

    I got the first part down no problem... when it comes to reading the string backward, looking for the first space or tab, then printing forward till last end or end of string, has got me stuck something terrible.. I have tried about 20 different ways to do it - and nothing seems to work just right...

    here is what I have... I need help with case 2 below...


    Code:
    /*
    Code by GoZippy
    This program reads in a user string, allows user to count occurance of each letter in string, display the string words in reverse order or quit. */
    
    #include <stdio.h>                            /* Include standard IO library */
    #include <string.h>                            /* Include stringh library */
    #define STR_LEN 100                            /* DEFINE String Length variable to 100 */
    
    
    int main (void)                                /* Declare and intialize variables */
    {
      char string[STR_LEN+1];                        /* Declare char string array " string " */
      int length;                            /* Declare and init length variable type integer */
      int menu;                                /* Menu varuable user input choice type integer */
      int i = 0;                            /* default generic loop position counter */
      int j = 0;                                /* second generic loop position counter*/ 
      int count[26] = {0};                            /* count array for alpha count */
      int pluscount = 0;                            /* forward count of word characters dilimited by tab or space                                             variable */
      int begin = 0;
      int end = 0;
    
      printf("Enter a text message: ");                    /* Prompt for text message*/
        for (i=0; i<STR_LEN; i++)
          {
            scanf("%c", &string[i]);
            if (string[i] == '\n')
              {
                length = i;
                i = 0;
                break;
              }
          }
    begin = length;
    end = length;
    
                                    /*Prompt for Menu Choices */
      printf("Available choices:\n");
      printf("   1. Display number of ocurrences of each letter\n");
      printf("   2. Print words in reverse order\n");
      printf("   3. Quit\n");
      printf("Enter the number of your choice: ");
        scanf("%i",&menu);
    
        switch(menu)
          {
            case 1:                            /* display ocurrence of each letter */
            j = 0;                            /* reset generic counter j to position 0 */ 
            while ( string[j] !='\0' && j <= length )        /* loop through string array positions */
              {
                if ( string[j] >= 'a' && string [j] <= 'z' )    /* if i is lowercase letter */
                    count[string[j]-'a']++;            /* get value for i, subtract 'a' then incriment corresponding count                                             array letter position 0-26 */
                else if ( string[j] >= 'A' && string[j] <= 'Z' )/* if i is upper case letter, subtract A then incriment                                         corresponding count array letter position */
                    count[string[j]-'A']++; 
                /*else 
                    printf("\n");
                    printf("There was an error with the input - CODE 101 \n"); */
                j++;
              }
            for (i=0;i<26;i++)
              {
                if (count[i] != 0)
                  {
                    printf("%c: %d \n",i+'a',count[i]);
                  }
              }
            break;
            case 2: /* print words in reverse order */
            for ( pluscount = 0, i = (strlen(string)-1); i >= 0; i--)
              {
                if ( (string[i] ==' ') || (string[i]=='\t') )
                  {
                        i++;
                        pluscount++;
                        while ((string[i] != ' ') && (string[i] !='\t') && (string[i] != '\n') && (string[i] != '\0') )
                          {
                              printf("%c", string[i]);
                              i++;
                              pluscount++;
                          }
                        i = (i-pluscount);
                  }        
                
                else if(i == 0)
                  {
                    begin = i;
                    for (j=begin; j<=end && (string[j] != ' ') 
                        && (string[j] != '\t') 
                        && (string[j] != '\n') 
                        && (string[j] != '\0'); j++)
                      {        
                        printf("%c",string[j]);
                     }
                    end = i;
                    printf("\n");
                      break;
                  }
    
              }
                
        
            case 3: /* quit - break from case and terminate */
            
            break;
          } 
    return 0;
    }
    Last edited by Eric Henderson; 02-23-2013 at 06:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone be my eyes for me?
    By NinjaFish in forum C Programming
    Replies: 3
    Last Post: 04-05-2011, 04:32 AM
  2. need a second pair of eyes
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 06-27-2002, 12:36 PM
  3. Need another set of eyes for debugging...
    By jstn in forum C Programming
    Replies: 5
    Last Post: 06-04-2002, 10:55 AM
  4. Prelude all Eyes on you
    By NewbieVB in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2002, 03:15 AM

Tags for this Thread