Thread: Problem with Strings and Conversions

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    Question Problem with Strings and Conversions

    Hi, I have a problem understanding how to convert a string of letters inputted from the user into the function toupper.
    ex. input: asdf

    output: ASDF

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define SIZE 50
    
    //void ToUpperCase(char, char);
    
    int main (void)
    {
    int i;
    char s1[SIZE];
    char s2[SIZE];
    
    printf("Please Enter the First String: ");
    scanf("%s", &s1);
    
    printf("\nPlease Enter the Second String: ");
    scanf("%s", &s2);
    
    printf("\n");
    
    for (i=0; i<=SIZE; i++)          /*Heres where problem is*/
    printf("%s,
    isalpha(s1[SIZE]) ? "Is " : "Is not ", "a letter");
    
    //ToUpperCase(s1, s2);
    
    return 0;
    }
    
    //void ToUpperCase(char, char){
    I don't understand how to get a scanned input into a function such as toupper or isalpha?
    Any help is greatly appreciated.!

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You need to run through each character in the string converting its case. You can loop through characters until you reach the \0 at which point you want to break it.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    through looping?

    How abouts would I do that?
    like this...
    for (i=0; i<=SIZE; i++)
    if (s1[i]?>>>>>?

    is this the approach, i understand how i would do this up till this point ?>>>>?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for (i=0; i<=SIZE; i++)
    you should stop when the nul- char is encountered
    also SIZE index is out of bounds
    so
    Code:
    for (i=0; s1[i] != '\0' && i < SIZE; i++)
    printf("&#37;s,
    isalpha(s1[SIZE]) ? "Is " : "Is not ", "a letter");

    you want access the i-th element

    Code:
    printf("%c is %sa letter", s1[i] , isalpha(s1[i]) ? "" : "not ");
    with toupper
    it will be something like
    s2[i] = toupper(s1[i]);

    and do not forget to copy the nul-char also
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    57

    I tried this but it comes out different.

    I placed this into my code but it comes out different, it only checks the first letter. I need it to check every letter inputted by the user.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Show your latest code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Drop the &'s when you read strings with scanf().

    Note that you're missing a double quote in your code. Since it evidently compiled, it leads me to think that you're just copying your code by hand. (Or maybe you just fixed it.) If the former is true, you should post your real code. There's always the chance that a problem in your code that gets lost when you transcribe it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    57
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define SIZE 50
    
    void ToUpperCase(char, char);
    
    int main (void)
    {
    int i=0;
    char s1[SIZE];
    char s2[SIZE];
     
    printf("Please Enter the First String: ");
    scanf("&#37;s", &s1[i]);
    
    
    printf("\nPlease Enter the Second String: ");
    scanf("%s", &s2); 
    
    printf("\n");
    
    
    
    for (i=0; s1[i] != ('\0' && i < SIZE); i++)
    printf("%c is %s letter", s1[i] , isalpha(s1[i]) ? "a" : "not a ");
    
    
     
    //ToUpperCase(s1, s2);
    
    return 0;
    }
    
    //void ToUpperCase(char, char){
    
    //int i;
    
    //for (i=0; i<=SIZE; i++)
    //printf("%s", s1
    //isalpha(s1[i]) ? "Is " : "Is not ", "a letter");
    I am so confused with this?

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    s1[i] != ('\0' && i < SIZE)
    That's not what you want. You're saying, "while the character I'm looking at is not equal to the truth of (zero and ...)". 0 && anything is always zero. So you're saying, basically,
    Code:
    s1[i] != 0
    Yes, that happened to work, but by fluke! Either use the above code directly or something like
    Code:
    s1[i] != '\0' && i < SIZE
    Note that you can ommit parameter names in prototypes, but not in function definitions. (Those are the ones with curly braces.)

    Code:
    scanf("&#37;s", &s1[i]);
    That happens to work, too, because i is zero. But really, all you need is this.
    Code:
    scanf("%s", s1);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading strings from a binary .txt file
    By rwmarsh in forum C++ Programming
    Replies: 8
    Last Post: 03-05-2006, 09:23 PM