Convert a char to uppercase

Hi,
I am trying to convert first letter of a word to upper case.With the below code it is working fine.I am able to convert to uppercase after finding the first period.If there is one more period in a sentence then the preceding char is still in lower case.

Ex:today it is too cold.its raining continuously.everyone should wear sweaters.

With the below code:
Today it is too cold.Its raining continuously.everyone should wear sweaters.

Below is the code:

size_t j=0;
for(size_t i=0;i<n.length();++i)
{
j=n.find('.'); //Finding the period from a string.

if(i==0)
{
if(islower(n[i]))
n[i]=toupper(n[i]);
}
else if(n[i]!=' ')
{
if(isupper(n[i]))
{
n[i]=tolower(n[i]);
}
}
else
{
i++;
if(islower(n[i]))
n[i]=toupper(n[i]);
}

if(j)
{
if(islower(n[j+1]))
n[j+1]=toupper(n[j+1]);

}
}

What are the necessary changes to be done for the above code?
closed account (o1vk4iN6)
Rather than doing this:

for(size_t i=0;i<n.length();++i)

Why not use a while loop like this:

1
2
3
4
5
6

std::string::size_type pos = 0;
while( (pos = str.find_first_of('.', pos)) != std::string::npos)
{
     str[ pos + 1 ] = toUpper( str[ pos + 1 ] );
}
Last edited on
Hi xerzi,
Thanks for your response.I tried ur code.If i use while loop how will it terminate from the loop?Also i wanted to convert the first char of all the letters in the sentence.Let me know if my understanding is wrong.
There is meant to be a space after a period in your ex.
In that case, if you find the period, n[i+2] has to be converted to upper case.
There is no need for the if (islower) statements and the first capitalization (when i==0)is a one off and does not need to be coupled with an else statement.
Hi Buffbill,
Sorry.I didnot get u.Actually i wanted to convert first letter of all the words in a sentence.With the above code it is working fine.Problem is existing when there is a period between the sentences.After period i am not able to convert the first char to upper.
closed account (o1vk4iN6)
You are using this code to find a period. This will only find the first one in the sentence so you are only going to do it for the first period in a sentence.

j=n.find('.');

Instead of using the find() function why don't you just add an if statement for a '.' since you are already looping through the entire string.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

size_t j=0; // should use std::string::size_type instead of size_t, altho size_t is the same type it here, that may not always be the case for other systems

for(size_t i=0;i<n.length();++i)
{
	j=n.find('.'); // will only get position of first period always...

	if(i==0)
	{
		if(islower(n[i])) // dont need these checks since the operation is rather simple you don't really save any time doing this
			n[i]=toupper(n[i]);
	}
	else if(n[i]!=' ')
	{
		if(isupper(n[i])) // here too
		{
			n[i]=tolower(n[i]);
		}
	}
	else
	{
		i++;
		if(islower(n[i])) // here too
			n[i]=toupper(n[i]);
	}

	if(j)
	{
		if(islower(n[j+1])) // here too
			n[j+1]=toupper(n[j+1]);

	}
}
Last edited on
To find the next char which is either a space or period, use string::find_first_of().
http://cplusplus.com/reference/string/string/find_first_of/

Oh, and do use somewhat less counter-intuitive variable names.
What were you thinking when you decided to use n as the identifier for a string?

The if if(islower(n[i])) n[i]=toupper(n[i]); is unnecessary, [/code]
See: http://www.cplusplus.com/reference/clibrary/cctype/toupper/

A more compact version of your code using string::find_first_of():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
typedef std::string::size_type pos_t ;
const char* const delimiters = ". " ;

inline pos_t next( const std::string& str, pos_t from = 0 )
{
    pos_t next_pos = str.find_first_of( delimiters, from ) ;
    return next_pos==std::string::npos ? str.size() : next_pos ;
}

int main()
{
  std::string str = "ToDay it is too COLD.It's raining continuously.everyone "
                    "should wear sweaters." ;

  pos_t curr = 0 ;
  pos_t end = next(str) ;

  for( pos_t curr = 0 ; curr < str.size() ; end = next(str,++curr) )
  {
      str[curr] = std::toupper(str[curr]) ;
      for( ++curr ; curr < end ; ++curr ) str[curr] = std::tolower(str[curr]) ;
  }

  std::cout << str << '\n' ;
}

Last edited on
Hi xerzi,
I tried by keeping an if condition in for loop for '.'.But it didnot work.

JLBorges:
Your logic is too complex for me to understand.
In English a period must be followed with a space or a new line. If you can understand this you will be able to understand my post.
Topic archived. No new replies allowed.