Thread: Need help capitalizing characters in a strong

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Need help capitalizing characters in a strong

    I'm trying to get to where the first letter of each word of the "description" is a capital letter while the rest is a lower case letter. So far, I'm not even able to successfully get every letter lower case. What am I doing wrong?




    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    
    
    #define STORE "Sierra Sporting Goods"
    #define MINPROD 1
    #define MAXPROD 9999
    #define MINTYPE 1
    #define MAXTYPE 5
    #define MINQUAN 1
    #define MAXQUAN 50
    #define MINCOST 5.00
    #define MAXCOST 900.00
    #define MINPRICE 6.00
    #define MAXPRICE 1000.00
    #define TRUE 1
    #define FALSE 0
    
    int menu(void);
    void add(void);
    int quit(void);
    void getstring(char entry[], char prompt[]);
    int getint(int min, int max, char msg[]);
    double getreal(double min, double max, char msg[]);
    double total(double t, double quantity);
    void show(int num, int product, int type, int quantity, double cost, double price, char description[]);
    void init_costs(double types[], int c);
    void show_costs(double types[], int c);
    char categories[][15] = {"", "Camping", "Tennis", "Golf", "Snow sports", "Water sports"};
    void strcase(char description[]);
    
    
    
    main()
    {
         int key, more = TRUE;
         do
        {
               key = menu();
               switch(key)
               {
                     case 1: add(); break;
                     case 2:
                     case 3:
                     case 4: printf("\nNot Implemented. Press Enter to Continue"); getchar(); break;
                     case 5: more = quit(); break;
                     default: printf("\nError in selection. Press Enter to Continue\a");
          getchar();
          }
    }
    while(more);
    return 0;
    }
    
    int menu()
    {
    system("cls");
    int selection;
    printf("%s\n\n\n", STORE);
    
              printf("1 = Add a record\n");
              printf("2 = Report\n");
              printf("3 = Delete a record\n");
              printf("4 = Change a record\n");
              printf("5 = Quit\n");
    
               /*For user selection*/
               printf("\n\nEnter your selection here: ");
               scanf("%d%*c", &selection);
    
    return(selection);
    }
    
    void add()
    {
         int n=0, product, type, quantity; /*Product is product number. Type is product type*/
         double cost, price, tprice; 
         char selection;
         char description[16];
    
         double types[6]; 
         
         init_costs(types, 6);
         
         do
         {
         system("cls");
         printf("%s\n\n\n", STORE);
         product = getint(MINPROD, MAXPROD, "Product Number");
         type = getint(MINTYPE, MAXTYPE, "Product Type");
         getstring(description, "Description:");
         quantity = getint(MINQUAN, MAXQUAN, "Product Quantity");   
         cost = getreal(MINCOST, MAXCOST, "Product Cost");
         price = getreal(MINPRICE, MAXPRICE, "Product Price");
         types[type] += cost;
         strcase(description);
         
         show(++n, product, type, quantity, cost, price, description);
         
         
         
         printf("\n\nDo you want to Continue? <Y or N>");
         scanf("%c%*c", &selection);     
              }
         while(selection == 'Y' || selection == 'y');
          
          
         system("cls");
         show_costs(types, 6); 
         printf("\nPress Enter to Continue"); 
         getchar();
    }
    
    void show(int n, int product, int type, int quantity, double cost, double price, char description[])
    {
         
    
         printf("\nTotal number of products> %d", n);
         printf("\nThe product number is --> %.4d", product);
         printf("\nThe product type is ----> %d", type);
         printf("\nThe Description is -----> %s", description);
         printf("\nThe quantity is --------> %d", quantity);
         printf("\nThe cost is ------------> %.2f", cost);
         printf("\nThe price is -----------> %.2f", price);
         printf("\nThe total price is -----> %.2f", total(price, quantity));
         printf("\nThe total cost is ------> %.2f", total(cost, quantity));
         
    }
    
    int getint(int min, int max, char item[])
    {
        int n;
        printf("Enter the %s %d to %d: ", item, min, max);
        scanf("%d%*c", &n);
        while (n < min || n > max)
          {
               printf("\nRange Error.\a");
               printf("\nEnter the %s %d to %d: ", item, min, max);
               scanf("%d%*c", &n);
          }
    return(n);
    }
    
    
    
    double getreal(double min, double max, char item[])
    {
           double n;
    printf("Enter the %s %.2f to %.2f: ", item, min, max);
    scanf("%lf%*c", &n);
    while(n < min || n > max)
            {
            printf("\nRange Error.\a");
            printf("\nEnter the %s %.2f to %.2f: ", item, min, max);
            scanf("%lf%*c", &n);
            }
    return (n);
    }
    
    
    
    double total(double t, double quantity)
    {
          return(quantity*t);
    }
    
    
    
    int quit()
    {
    printf("\nPress Enter to Continue");
    getchar();
    return 0;
    }
    
    
    
    void init_costs(double types[], int c)
    {
         int i;
         
         for(i=1; i<c; i++)
         types[i] = 0;
    }         
    
    void show_costs(double types[], int c)
    {
         int i;
         
         printf("%s\n\n\n", STORE);
         printf("Product Cost by Type");
         printf("\n\nType_______Category_______Cost\n\n");
         for(i=1; i<c; i++)
         {
         printf("%d%18s%10.2lf\n", i, categories[i], types[i]);
    }
    }
    
    void getstring(char entry[], char prompt[])
    {
         printf("Enter the %s", prompt);
         gets(entry);
    }
    
    void strcase(char description[])
    {
         int i;
         
          for (i = 0; i < strlen(description); i++)
    	{
    		description[i] = tolower(description[i]);
    	}
          gets(description);
    }
    Last edited by zbred; 03-25-2011 at 04:35 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok, please re-edit your code to inlcude code tags and make sure it's indented in a readable style...
    << !! Posting Code? Read this First !! >>

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    my bad

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    void strcase(char description[])
    {
         int i;
         
          for (i = 0; i < strlen(description); i++)
    	{
    		description[i] = tolower(description[i]);
    	}
          gets(description);  <--- and what's that doing in there?
    }

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    So far, I was able to get to strcase() funtion to work by changing it from a void to a char. I just need help at this point to get the words to look like this:

    Blah Blah Blah

    (and not like)

    bLAh bLAH BLAh

    Code:
    char strcase(char description[])
    {
         int i;
         
         
          for (i = 0; i < strlen(description); i++)
    	{
    		description[i] = tolower(description[i]);
    	}
     return(description);
    }
    The mistake in the get(description) was from lack of understanding, I guess. I don't know.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You shouldn't use gets. Your problem is here however:
    Code:
    void strcase(char description[])
    {
         int i;
         
          for (i = 0; i < strlen(description); i++)
    	{
    		description[i] = tolower(description[i]);
    	}
          gets(description);
    }
    After you lower the string, you immediately overwrite it by prompting them to enter the description again.

    edit - beaten to the punch

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zbred View Post
    So far, I was able to get to strcase() funtion to work by changing it from a void to a char. I just need help at this point to get the words to look like this:

    Blah Blah Blah

    (and not like)

    bLAh bLAH BLAh

    Code:
    char strcase(char description[])
    {
         int i;
         
         
          for (i = 0; i < strlen(description); i++)
    	{
    		description[i] = tolower(description[i]);
    	}
     return(description);
    }
    The mistake in the get(description) was from lack of understanding, I guess. I don't know.
    You also don't need to return description. Technically you can't return an array.

    Try this...
    Code:
    void strcase(char description[])
    {
         int i;
         for (i = 0; i < strlen(description); i++)
           {
              description[i] = tolower(description[i]);
            }
    }
    You are passing in the pointer to your description. The function will edit it in place.

    One tip, as a matter of efficiency, it is very slow to have your loop recalculate strlen() on each iteration...
    Code:
    void strcase(char description[])
    {
         int i;
         int len = strlen(description);
         for (i = 0; i < len; i++)
           {
              description[i] = tolower(description[i]);
            }
    }
    It probably won't make a noticeable difference on short strings... but on long ones it makes quite a difference.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Code:
    void strcase(char description[])
    {
         int i;
         int len = strlen(description);
         for (i = 0; i < len; i++)
           {
              description[i] = tolower(description[i]);
            }
    }
    That definitely helped it all work efficiently. Now how would I find the first letter of each word and change it to a capital letter?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zbred View Post
    Code:
    void strcase(char description[])
    {
         int i;
         int len = strlen(description);
         for (i = 0; i < len; i++)
           {
              description[i] = tolower(description[i]);
            }
    }
    That definitely helped it all work efficiently. Now how would I find the first letter of each word and change it to a capital letter?
    Give this a look.... capitalizing only first letter in strings??

    Are you guys in the same class?
    Last edited by CommonTater; 03-25-2011 at 04:56 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by zbred View Post
    Now how would I find the first letter of each word and change it to a capital letter?
    How do you know when a new word starts?


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    AHEM... Did you not see my original example?

    C'mon... put thinking cap on before applying fingers to keyboard!
    If you're talking about this code, then wouldn't it just lower case every letter?
    Code:
    void strcase(char description[])
    {
         int i;
         for (i = 0; i < strlen(description); i++)
           {
              description[i] = tolower(description[i]);
            }
    }
    How do you know when a new word starts?
    That's what I'm not sure about. I was thinking about finding the first letter of the string array description[0] and forcing that to be capitalized then finding where there's a space and capitalizing the next letter. However, I do not know what code i would use to find to letter with the space.
    Last edited by zbred; 03-25-2011 at 05:01 PM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zbred View Post
    If you're talking about this code, then wouldn't it just lower case every letter?
    Code:
    void strcase(char description[])
    {
         int i;
         for (i = 0; i < strlen(description); i++)
           {
              description[i] = tolower(description[i]);
            }
    }
    No, sorry... there are two almost identical threads running. I posted an example in the other one and then promptly lost track of who's who...

    Take a look at this...
    capitalizing only first letter in strings??

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    No, sorry... there are two almost identical threads running. I posted an example in the other one and then promptly lost track of who's who...

    Take a look at this...
    capitalizing only first letter in strings??
    That thread definitely helped me. Now i have the very first word capitalized, but I'm messing up getting each new word capitalized


    Code:
    void strcase(char description[])
    {
         int i;
         for (i = 0; i < strlen(description); i++)
           {
              description[i] = tolower(description[i]);
            }
        while (description[i] = ' ')
        {
              ++i;
             description[i] = toupper(description[i]);
    }
               description[0] = toupper(description[0]);
    }
    I just need help with the while function in there. What am I doing wrong?

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by zbred View Post
    That thread definitely helped me. Now i have the very first word capitalized, but I'm messing up getting each new word capitalized

    Code:
    void strcase(char description[])
    {
         int i;
         for (i = 0; i < strlen(description); i++)
           {
              description[i] = tolower(description[i]);
            }
        while (description[i] = ' ')
        {
              ++i;
             description[i] = toupper(description[i]);
    }
               description[0] = toupper(description[0]);
    }
    I just need help with the while function in there. What am I doing wrong?
    Did you see this?

    Code:
    void strcase(char *str)
      { int x, y;  // loop variables
         char *ucp;  // upper case pointer 
         y = strlen(str);
         // convert to all lower case
         for( x = 0;  x < y; x++)
           {  str[x] = tolower(str[x]); }
        // capitalize words
       str[0] = toupper(str[0]);
       ucp = str;
        while (ucp = strchr(ucp,' '))
           { ++ ucp;
               *ucp = toupper(*ucp);}  }
    The key to making it work is the strchr function, you chose not to include...

  15. #15
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Honestly, I don't know how to make the pointer ucp work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM