Ever just need a second set of eyes to help?

This is a discussion on Ever just need a second set of eyes to help? within the C Programming forums, part of the General Programming Boards category; For some reason I decided to go back to school to get my degree in this stuff... I am taking ...

  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.

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    for instance - with user input
    This is a String

    it should print

    String a is This

    ....
    ...
    I do not want to use pointers in this just yet - this is supposed to be basic but I am missing something...

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,632
    When going backward through the string, you will find the end of each substring first and then need keep going until you find its beginning. Once you have the beginning and end, simply print everything between the identified beginning and end. Then remember to add a space after printing each substring (your code doesn't appear to be doing that).


    You can probably simplify your code a lot by making use of functions from the standard library. The standard header <ctype.h> specifies functions like isalpha() to test is a character is a letter, islower() to check for lower case, isupper() to check for upper case, isspace() to check for whitespace (space, tab, newlines, etc), and a bunch of other things.


    One thing to watch with your code is that, when reading, you are not adding a zero terminator to the string. That means subsequent code cannot assume it will find a zero terminator. strlen() searches for a zero terminator, and gives undefined behaviour (walking over all memory until it finds a character with value zero) if one is not there. You're probably getting by on that because your compiler is initialising string to zero - problem is, compilers are NOT required to do that.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

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, 05:32 AM
  2. need a second pair of eyes
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 06-27-2002, 01:36 PM
  3. Need another set of eyes for debugging...
    By jstn in forum C Programming
    Replies: 5
    Last Post: 06-04-2002, 11:55 AM
  4. Prelude all Eyes on you
    By NewbieVB in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2002, 04:15 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21