function to change case without using array

May 11, 2013 at 8:59pm
How would you write a function to change the case of letters in a name say "bob s. smith,karen b. wright" into "Bob S. Smith,Karen B. Wright" without using any c type functions besides to upper? This would be easy if I were to write it using arrays, but I need to process the string using c++ type.

May 11, 2013 at 9:11pm
you can use an iterator to iterate the string and on meeting a whitespace character(spacebar), it copies the next letter, changes it to uppercease, constructs a new string and return it.

Aceix.
May 11, 2013 at 9:27pm
You could do something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
int main ( void )
{
    std::string str("this is just a test string blah a a a a a   la     lal asdf.");
    
    std::cout << str << std::endl;

    if( str[ 0 ] > 96 )
    {
        str[ 0 ]  = toUpper( str[ 0 ] );
    }

    for(unsigned int i = 1; i < str.size(); i++ )
    {
        if( str[ i ] == ' '  && str[ i+1 ] != ' ' && str[ i+1 ] > 96 )
        {
            str[ i+1 ] = toUpper( str[ i+1 ] );
        }
    }

    std::cout << str << std::endl;

    return( 0 );
}
this is just a test string blah a a a a a   la     lal asdf.
This Is Just A Test String Blah A A A A A   La     Lal Asdf.

Process returned 0 (0x0)   execution time : 0.320 s
Press any key to continue.


If you use anything other than a letter or space after a space though it will not work ex something like "test \ [ a \"

I honestly think though it would be best to use toUpper after each space and for the first character...
Last edited on May 11, 2013 at 11:41pm
May 11, 2013 at 10:25pm
int main()
{
std::string str = "jane r. white, jake t. great";

std::transform(str.begin(), str.begin()+1, str.begin(), std::ptr_fun(toupper));
std::cout << str << std::endl;
}

This would capitalize the first letter in the string. Is there a way to set the conditions to capitalize after " " or "," ? I am totally new to programming in general and have been search for a while.
May 11, 2013 at 10:39pm
use a for loop and find the spaces and comma's
ex:
1
2
3
4
5
6
7
8
9
std::string str = "jane r. white, jake t. great";
unsigned int pos = 0;
for(unsigned int i = 0; i < str.size(); i++ )
{
     if( str[i] == ' ' || str[i] == ',' )
     {
          pos = i;
     }
}

May 11, 2013 at 11:01pm
Gibilt, you are using type strings since it is using arrays, but I need to use c++ type strings. That would be so much easier to use your method.
May 11, 2013 at 11:39pm
strings are c++ I am pretty sure of because in c they use arrays of characters (which a string is basically) and all I did was check each character in the string if it was a space or a comma and my code isn't very hard it's only 4 lines of code besides the brackets and the original string
and it looks to me like you want an array of strings because it seems like those should be two different names
e
like
 
std::string str[2] = { "jane r. white" , "jake t. great" };

seems kind of pointless to have an array of strings in one string then break them apart by searching for the comma
and I am editing my oiginal post I was thinking you couldn't use toUpper but looks like you can.
Last edited on May 11, 2013 at 11:40pm
May 11, 2013 at 11:44pm
Oh now I understand! Thanks so much! So it depends on the way you declare the variable. I was wondering how someone would do this without checking each character alone. Thanks again!
May 12, 2013 at 6:34am
How would I do this using string concatenations?
Topic archived. No new replies allowed.