convering lowercase to upper case and percent to decimal

This is a discussion on convering lowercase to upper case and percent to decimal within the C Programming forums, part of the General Programming Boards category; For the case converting, would it be something like this? (Im trying to converst just the first letter): Code: include ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    21

    convering lowercase to upper case and percent to decimal

    For the case converting, would it be something like this? (Im trying to converst just the first letter):

    Code:
    include <stdio.h>
    include <ctype.h>
    
    int main()
    {    
        char first_name[26];
        printf("Enter last name:\n");
        printf("----------------------\n");
        fgets(line, 500, stdin);
        sscanf(line, "&#37;s", last_name);
        printf("\n");
    
        first_name = toupper(first_name);
    
    
    return 0;
    }
    asfor the percentage converion, i have no idea how to get started on that, any advice on where to look to get some ideas?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Percent means hundredths, so 20.0% is .200

    Take your percent number, divide it by 100 and add the decimal.

    Adak

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    Code:
    include <stdio.h>
    include <ctype.h>
    
    int main()
    {    
        char first_name[26];
        printf("Enter last name:\n");
        printf("----------------------\n");
        fgets(line, 500, stdin);
        sscanf(line, "&#37;s", last_name);
        printf("\n");
    
        first_name = toupper(first_name);
    
    
    return 0;
    }
    Where are line and last_name declared? To answer your question, no it's not right. You didn't put much effort into making it even compile.

    To change just the first character of a string to uppercase try:
    Code:
    name[0] = toupper(name[0]);
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    Oh sorry man, this is actually a part of a much larger code. I just tried to take out the important part of the code that applies to the case conversion. I wasn't trying to compile that one part on its own.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    21
    for the decimal conversion would this work:

    Code:
     if (*apr[9]==%)
        {
        *apr = (*apr)/100;//if apr contains a percent sign, it will be converted to decimals            
                    }

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    [smart remark]Do we look like compilers to you?[/smart remark]



    Some problems:

    • First of all, no idea what type apr is. If it's an array of chars, then you don't need to dereference it if you're using an index, since that is automatically done.
    • You can't compare with &#37; like that. Use '%' for it to be interpretted as a char.
    • If you dereference an array, all you're doing is getting the value that it points to, which in the case of the array is the first value of the array. This means for a char array, you're getting the first char, dividing by 100 (using integer division!!!!) and then storing back inside that same first char


    Go back and read up on arrays, strings, and pointers.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by cboard View Post
    I wasn't trying to compile that one part on its own.
    Try.

    Debugging fake code is harder than debugging real code. Post a minimal compilable snippet that demonstrates the issues you have and you will get a full and complete answer much faster than playing 20 questions with, "it's kinda like this, in a way".
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

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