Thread: Need help with capitalizing char strings

  1. #16
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Quote Originally Posted by sndpchikane View Post
    Hey ChotaChatri!!

    Are you from India?
    Yup from India studying in 2nd year
    Why?

  2. #17
    Registered User
    Join Date
    Mar 2008
    Posts
    15

    Works good

    Yep, works good now, thanks.

    I'm going to work on tweaking it with flag and isspace so I can make it so I code in the prose and then have the code automatically switch the first letters to capitals.

    I want to get it so I code in: to be or not to be that it the question
    And have it spit out
    To Be Or Not To Be That Is The Question
    in all one interation and have both lines print out.

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by chottachatri View Post
    Please let me know if this is working or not!
    What do you think will happen if the last char is space?
    What about treating \t and \n as spaces like isspace does?
    what about using toupper to make you program readable and portable?

    and of course using user entered string as a format for printf is always bad idea... there is puts function, or if you do not want a new line char at the end:
    printf("%s",s);
    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

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Quote Originally Posted by vart View Post
    What do you think will happen if the last char is space?
    What about treating \t and \n as spaces like isspace does?
    what about using toupper to make you program readable and portable?

    and of course using user entered string as a format for printf is always bad idea... there is puts function, or if you do not want a new line char at the end:
    printf("%s",s);
    First of all \n wouldn't at all come as i have taken a single string only! i hope you understand secondly which sentence in English has a space as it's last character??and by the way it's running for that. You may check it out ! and then criticise!

    Thirdly in which english sentence do we encounter tab??if a english sentence has a tab in between , is it a proper english sentence???

    if it all you want to make the program fancy only little conditions need to be put. Nothing extra-ordinary you have to do. I just gave basic idea regarding how to solve it. That fancy kinda stuff he would be able to do it once he knows this basic concept. And why shouldn't i use magic constants? the ascii characters of A to Z and a to z wil remain this only forever. Neither you nor i would be able to change it. Why do you stress the use of toupper god only knows! and moreover toupper function isn't going to do something else. It will also check the ascii characters only and if its between 97 and 122 it will decrease it's value by 32


    --Chotti
    Last edited by chottachatri; 03-23-2008 at 01:27 AM.

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and by the way it's running for that.
    Oh, yeah. It is running for that... and continues running out of bound of the array...
    the ascii characters of A to Z and a to z wil remain this only forever
    Not all computers use ASCII encoding... that's why using magic numbers makes your program non-portable... That's why functions like isspace, toupper, islower exist...
    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

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Hey dude what exactly are you saying?? :d Please atleast check the program before you make comments on it!
    why will it run out of bound??i have used the s[i]!='\0' condition in for loop if you can see it! and look at the if condition if it's a space it will increment by 1 so if you say last character is space it will now point to '\0' character in your case and the condition in for loop then terminates the loop so what exactly are you saying??

    Please run it if still you have any doubts

    Ask JoelearningC whether his computer is ascii encoding or not! and if it is then the matter i believe is over here only!

  7. #22
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by chottachatri View Post
    Hey dude what exactly are you saying?? :d Please atleast check the program before you make comments on it!
    why will it run out of bound??i have used the s[i]!='\0' condition in for loop if you can see it! and look at the if condition if it's a space it will increment by 1 so if you say last character is space it will now point to '\0' character in your case and the condition in for loop then terminates the loop so what exactly are you saying??

    Please run it if still you have any doubts

    Ask JoelearningC whether his computer is ascii encoding or not! and if it is then the matter i believe is over here only!
    Ooooh, we're using integer constants instead of glyphs. Good for you, you've [needlessly?] mastered the ascii table. You should never have needed to in the first place.

    What is so wrong with using a simple, natural literal like 'a' instead of 64? For your sake, let's pretend that all the other possible character encodings of interest disappeared, such as ISO Latin-1, EBDIC (the traditional encoding for the IBM mainframe), UTF-8, UTF-16 and Unicode. For all intents and purposes ascii will be all there ever is or used. Now 64 is portable, but at what cost? Was making corresponding integer constants work really worth it?

    Seems like we piled on yet another confusion when learning C, when people start needing to explain what integer constants mean in glyph contexts. Don't do this to noobs.
    Last edited by whiteflags; 03-23-2008 at 03:03 AM.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chottachatri
    And why shouldn't i use magic constants? the ascii characters of A to Z and a to z wil remain this only forever.
    Quick, what's the ASCII value of 'r'? What about 'M'? '.'? '?'? If you took half a second to correctly answer each of these questions without referring to the ASCII table, you are almost half a second too slow. This is a reason why you should not use magic constants even if ASCII is guaranteed: you would need to memorise the whole ASCII table, or spend some time looking it up.

    Quote Originally Posted by citizen
    What is so wrong with using a simple, natural literal like 'a' instead of 64?
    'A' is 65, 'a' is 97, which illustrates your point
    Last edited by laserlight; 03-23-2008 at 03:26 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Instead of remembering 97 and 122 you can just do this:
    Code:
    if (flag==1 && s[i] >= 'a' && s[i] <= 'z')
    EDIT: sorry i didnt see the second page which already mentions this.
    Last edited by 39ster; 03-23-2008 at 04:45 AM.

  10. #25
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    hey the point is not about s[i]>='a' && s[i]<='z'
    the point is that the difference between a and A is 32!! that was what vart was talking about and we dont need to remember the ascii values of each and every character! just remember that the difference between a and A is 32 that's more than enough
    i am comfortable with that so i will program like that. If you are not comfortable then you may not use it!simple! what's the point of arguing so much?

  11. #26
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    I didnt read the second page. My suggestion had nothing to do with what was said on the second page.
    Last edited by 39ster; 03-23-2008 at 04:56 AM.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hey the point is not about s[i]>='a' && s[i]<='z'
    It is also about that.

    the point is that the difference between a and A is 32!! that was what vart was talking about and we dont need to remember the ascii values of each and every character! just remember that the difference between a and A is 32 that's more than enough
    I believe vart meant more than that. 32 is also a magic constant. You do not even need to remember that the difference between 'A' and 'a' is 32 if you are willing to pre-calculate 'a'-'A', assuming we are using ASCII or some other character set that has a similiar layout of the alphabet. You do not even have to make that assumption when pre-calculating 'a'-'A' if you use toupper() and thus not need to do anything yourself.

    i am comfortable with that so i will program like that. If you are not comfortable then you may not use it!simple!
    That is fine if your code is only ever read by yourself.

    what's the point of arguing so much?
    This is a question of "best practice". You posted code as a suggestion, so expect it to be picked apart if it teaches bad practices.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User Nagash's Avatar
    Join Date
    Sep 2008
    Location
    Brazil - SC, Florianopolis
    Posts
    3
    Hey guys do u know a way I can capitalize everything writen in a specific type... for example:
    Code:
    typedef char Str[50]; //I want everything with this type to be TOUPPER
    
    typedef struct {
    	Str title, director, genre, year, available, code; // all will be in a struct
    	}MOVIE;
    I couldn't manage to make common toupper when I read them... the following allways shows an error: "request for member `title' in something not a structure or union"
    Code:
    void movielist (MOVIE list[50]){
        
    	puts("Type in the movie Title:");
    	gets((*list).title);
                    list.title=toupper(list.title);   
    }
    Isn't there a way I could allready toupper as soon as I declared my Str[50] or even the struct MOVIE?
    Last edited by Nagash; 09-19-2008 at 06:59 AM.

  14. #29
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Nagash - please do not bump old threads; just start a new one to post your question to. I'm not exactly clear on your exact question, but the error may stem from the fact that 'title' is an array of char and you are attempting to pass it to toupper(), which expects a single character (hint: use a loop).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #30
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by citizen View Post
    Don't do this to noobs.
    http://www.ctrlaltdel-online.com/comic.php?d=20060823

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. help with strings
    By webbizdesign in forum C Programming
    Replies: 5
    Last Post: 11-13-2003, 08:20 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM