Thread: Changing characters in a string.

  1. #1
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115

    Changing characters in a string.

    Hi, I’m trying to write a simple program to change the charters in a string, but for some reason I cant get it to work, and I cant for the life of me work out why, im fairly new to c++ so im sure must be doing some stupid mistake, but if someone could have a look at this code, and give me a pointer or 2, it would be greatly appreciated.

    Basicly all i want it to do is change all the "x"'s to an upper case X...

    it will change the first one, but thats it...

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        string sen;
        cout << "Please enter a sentence ";
        getline (cin, sen);
        string::size_type senx = sen.find( "x" );
        if (senx >=0)
        {
                 sen[senx] = toupper (sen[senx]);
                 string::size_type senx = sen.find( "x" );
        }
    
        cout << sen << endl;
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You don't have a loop. You are using if, but if doesn't loop, it just runs once. You probably want a while loop.

  3. #3
    Registered Abuser Loic's Avatar
    Join Date
    Mar 2007
    Location
    Sydney
    Posts
    115
    Yeah, i realized that,..

    but i got it working, now i am just trying to compact it a bit, i have a few ideas, but just wanted to get some advice...

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        string sen;
        cout << "Please enter a sentence ";
        getline (cin, sen);
        // Change X
        int countx = 0;
        for (int i = 0; i < sen.size(); ++i)
        if (sen[i] == 'x')
              ++countx;
        int x=0;
        while (x <= countx)
        {
              string::size_type senx = sen.find( "x" );
              sen[senx] = toupper (sen[senx]);
              x++;
        }
        // Change Y
        int county = 0;
        for (int i = 0; i < sen.size(); ++i)
        if (sen[i] == 'y')
              ++county;
        int y=0;
        while (y <= county)
        {
              string::size_type senx = sen.find( "y" );
              sen[senx] = toupper (sen[senx]);
              y++;
        }
        // Change Z
        int countz = 0;
        for (int i = 0; i < sen.size(); ++i)
        if (sen[i] == 'z')
              ++countz;
        int z=0;
        while (z <= countz)
        {
              string::size_type senx = sen.find( "z" );
              sen[senx] = toupper (sen[senx]);
              z++;
        }
        // output    
        cout << sen << endl;
    
        system("PAUSE");
        return EXIT_SUCCESS;
    }

  4. #4
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Maybe something along the line of:

    Code:
        char replace[] = {'x', 'y', 'z'};
        int i, pos;
        string sen;
    
        cout << "Please enter a sentence ";
        getline (cin, sen);
    
        for (i = 0; i < sizeof(replace); i++)
        {
            while ((pos = sen.find(replace[i])) != string::npos)
            {
                sen[pos] = toupper(sen[pos]);
            }
        }
        cout << sen;
    replace is a character array that contains all the characters you want to capitalize. I first iterate over all the characters inside that array, to have them all treated. Then I replace the characters as long as I found one (sen.find returns string::npos if it couldn't find any occurence).
    Last edited by KONI; 03-30-2007 at 05:17 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. populating a string only alpha characters
    By MSF1981 in forum C Programming
    Replies: 10
    Last Post: 02-06-2009, 10:58 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Reading characters from a string?
    By ladysniper in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 11:45 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM