Thread: learning :) help would be nice

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    51

    Wink learning :) help would be nice

    hi i just learned pointers kind of sort of, and the book gave me this example....


    #include <iostream>

    using std::cout;
    using std::endl;

    #include <cctype>

    void convertToUppercase( char * );

    int main()
    {
    char string[] = "characters and $32.98";

    cout << "The string before conversion is: " << string;
    convertToUppercase( string );
    cout << "\nThe string after conversion is: "
    << string << endl;
    return 0;
    }

    void convertToUppercase( char *sPtr )
    {
    while ( *sPtr != '\0' ) {

    if ( islower( *sPtr ) )
    *sPtr = toupper( *sPtr ); // convert to uppercase

    ++sPtr; // move sPtr to the next character
    }
    }

    i guess this converts the string, characters and $32.98 to upper case, i was just wondering, how could i manipulate which characters get capitolized and which characters dont? just a question my book didnt cover

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >how could i manipulate which characters get capitolized

    It depends exactly what mean by manipulate which characters get capitalised.

    You could split your original string up & capitalise certain sections of it, and recombine it aftwerwards.

    Alternatively, you could modify the algorithm to suit what you want to do. For example, you could change it so it only capitalises the starting character of each word. Therefore the string:

    "hello world"

    becomes

    "Hello World"

    To do this you will have to check for space characters, and only capatilise the character after. Also capitalise the first character always.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    51

    ok...

    hmm i see, i think i didnt make my question concise enough. lets clarify


    say i want to print....


    " c programming is fun! "

    and i want it to capitolize this way...

    "C programming IS fun"

    see? i m just curious, how this would be accomplished

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I'll say it before the mods get on your case: please use code tags, make life a LOT easier for us to read your code!

    Second, this code only makes to uppercase whatever the pointer is pointing to at the time, in this case every letter because the while loop cycles from the beginning to the end hitting every letter. If you want to do every other character, change
    Code:
    ++sPtr;
    to
    Code:
    sPtr+=2;
    To pick and choose a little more, you could do something like:
    Code:
    void convertToUppercase( char *sPtr, int whichOne )
      {
         *sPtr = toupper( *sPtr + whichOne - 1)
      }
    if you then wrote convertToUppercase(string, 5) it would uppercase only the 5th character. Not saying that this is the ideal way to do this, but hopefully this will you give you an idea of whats going on. The pointer points to the first character in the string (or the address of technically) and each time its incremented it points to the next letter in the string.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    PJ maybe you should check operator precedence rules. The +whichOne is added to the pointee and not the pointer itself.Not the behaviour you were expecting no doubt.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >say i want to print....

    >" c programming is fun! "

    >and i want it to capitolize this way...

    >"C programming IS fun"

    I think I follow you now.

    You could modify your function so that it specifies a start and range area of the string you want to captilise. Something like:

    Code:
    void convertToUppercase( char *sPtr, int start, int cnt )
    {
      int pos = start;
      while ( pos < start + cnt )
      {
        if ( islower( sPtr[pos] ) )
          sPtr[pos] = toupper( sPtr[pos] ); // convert to uppercase
    
        pos++;
      }
    }
    I've haven't compiled this, so there maybe one or two glitches, but the gist of it should be there. I've also use a slight variation of for de-referencing the string pointer on a per char basis because I find this easier, but it should make no difference overall.

    Now if your string was:

    "c programming is fun!"

    calling:

    Code:
    convertToUpperCase(strPtr, 14, 2);
    should change to strPtr to

    "c programming IS fun!"

    also calling convertToUpperCase(strPtr, 0, 1);

    should convert your start character, so that it becomes

    "C programming IS fun!"

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    PJ maybe you should check operator precedence rules. The +whichOne is added to the pointee and not the pointer itself.Not the behaviour you were expecting no doubt
    Whoops, you're right, forgot the extra parenthesis there! Shoulda been:
    Code:
         *sPtr = toupper( *(sPtr + whichOne - 1));
    And depending on whether or not its easier to think of the first character as spot 1 or spot 0 depends on whether or not you care about the "- 1" part.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    51
    i understand your logic, but hwne i compiled i got this


    y Documents\pointerpractice.cpp(17) : error C2065: 'sPtr' : undeclared identifier.
    i dont really understand how its undefined???

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    51
    for some reason its not letting me compile if i write convertToUppercase ( string, 5 ) or anything of the sort. do i need a special header file???

  10. #10
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Post your code again, its hard to say whats going on without seeing it.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    51
    ok sorry


    #include <iostream>

    using std::cout;
    using std::endl;

    #include <cctype>

    void convertToUppercase( char * );

    int main()
    {
    char string[] = "c programming is fun!";

    cout << "The string before conversion is: " << string;
    convertToUpperCase(strPtr, 14, 2); // error started here
    cout << "\nThe string after conversion is: "
    << string << endl;
    return 0;
    }

    void convertToUppercase( char *sPtr, int start, int cnt )
    {
    int pos = start;
    while ( pos < start + cnt )
    {
    if ( islower( sPtr[pos] ) )
    sPtr[pos] = toupper( sPtr[pos] ); // convert to uppercase

    pos++;
    }
    }

    and i want it to return " C programming IS fun!"

  12. #12
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Ahhh.. he changed variable names on ya

    Change this:
    Code:
    convertToUpperCase(strPtr, 14, 2); // error started here
    to this:
    Code:
    convertToUpperCase(string, 14, 2); // error started here
    and that will change the is to IS - if you want to change the c to C you need to make another call your function with parameters 0 and 1 instead of 14 and 2.

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctype>
    
    using std::cout;
    using std::endl;
    
    inline void pause() { system("PAUSE"); };
    
    std::string convertToUpperCase( std::string, int);
    
    int main()
    {
    std::string str = "c programming is fun!";
    
    cout << "The string before conversion is: " << str;
    
    str = convertToUpperCase(str, 0); // error started here
    
    cout << "\nThe string after conversion is: " << str << endl;
    
    pause();
    return 0;
    }
    
    std::string convertToUpperCase( std::string str, int start)
    {
    unsigned int pos = start;
    
        while ( pos < str.length() ){
            if ( islower( str[pos] ) ) {
                str[pos] = toupper( str[pos] ); // convert to uppercase
            }
        pos++;
        }
    return str;
    }
    Sorry, but this is the best I can do with your code. As you'll see, it converts all of your characters to 'uppercase'.

    The problem? You need a find() within the code for a sequence that is very specific. It can be done, but, probably not in a practical fashion.

    You'd need a great deal more code, here, and, from what you've given us, we can "fake" it, but it has no practical use for anything beyond a one-time application.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is learning C/C++ worth it?
    By C/C++ Learner in forum A Brief History of Cprogramming.com
    Replies: 44
    Last Post: 11-05-2008, 03:55 PM
  2. learning ability...gone?
    By jessie23 in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-08-2003, 04:02 PM
  3. Learning Rate Of C++
    By Krak in forum C++ Programming
    Replies: 27
    Last Post: 01-29-2003, 01:53 PM
  4. learning to code
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-30-2001, 08:49 AM
  5. Looking for good beginner tutorials in learning windows api
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 10-30-2001, 06:42 AM