# convert a string into capital letter using a function

• (2 Pages)
• 1
• 2

## 24 Replies - 4972 Views - Last Post: 01 September 2012 - 12:19 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=290621&amp;s=b5f7ff9cef451e8c2fee8c0125214932&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jgar

Reputation: 0
• Posts: 16
• Joined: 31-August 12

# convert a string into capital letter using a function

Posted 31 August 2012 - 07:31 PM

Hello,

I am having difficulties in converting a string into capital letters..
how can I do it?
Is This A Good Question/Topic? 0

## Replies To: convert a string into capital letter using a function

### #2 CTphpnwb

• D.I.C Lover

Reputation: 3826
• Posts: 13,945
• Joined: 08-August 08

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 07:32 PM

### #3 jgar

Reputation: 0
• Posts: 16
• Joined: 31-August 12

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 07:45 PM

I have tried this

```void capital(std::string &name)

{
int x;
for(x = 0; x <= name.size()-1; x++)

if((name.at(x) >= 'a') && (name.at(x) <= 'z'))

name.at(x) -= 32;
}
```

but when I debug it, it gives me an error..
: warning C4018: '<=' : signed/unsigned mismatch
pointing to the for loop

This post has been edited by jimblumberg: 31 August 2012 - 07:48 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.

### #4 jimblumberg

Reputation: 5782
• Posts: 17,685
• Joined: 25-December 09

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 07:50 PM

What is the type of variable returned from the std::string.size() function? Is it an int?

Jim

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3826
• Posts: 13,945
• Joined: 08-August 08

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 07:56 PM

It works for me.
```#include <iostream>
#include <string>

void capital(std::string &name)
{
int x;
for(x = 0; x <= name.size()-1; x++)

if((name.at(x) >= 'a') && (name.at(x) <= 'z'))

name.at(x) -= 32;
}

int main(int argc, const char * argv[])
{
std::string test;
test = "hello world!";
capital(test);
std::cout << test <<std::endl;
return 0;
}
```

### #6 jgar

Reputation: 0
• Posts: 16
• Joined: 31-August 12

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 07:57 PM

jimblumberg, on 31 August 2012 - 07:50 PM, said:

What is the type of variable returned from the std::string.size() function? Is it an int?

Jim

The program does not run... what I am trying to do is to convert let's say for example
a name ---- alex into ALEX....
using a function that returns the whole string into capital letters.

### #7 Skydiver

• Code herder

Reputation: 7192
• Posts: 24,369
• Joined: 05-May 12

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:01 PM

Change line 4 to:
```decltype(name.size()) x;

```

to get rid of the warning.

But beware that a logic bug persists on line 5. Notice what happens if size() returns 0?

You'll need to change the loop condition to use less than rather than less than or equal, and change the loop limit.

This post has been edited by Skydiver: 31 August 2012 - 08:06 PM

### #8 jgar

Reputation: 0
• Posts: 16
• Joined: 31-August 12

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:03 PM

CTphpnwb, on 31 August 2012 - 07:56 PM, said:

It works for me.
```#include <iostream>
#include <string>

void capital(std::string &name)
{
int x;
for(x = 0; x <= name.size()-1; x++)

if((name.at(x) >= 'a') && (name.at(x) <= 'z'))

name.at(x) -= 32;
}

int main(int argc, const char * argv[])
{
std::string test;
test = "hello world!";
capital(test);
std::cout << test <<std::endl;
return 0;
}
```

Thanks JIM ... it works now... I was missing the std:: in the main function.

### #9 Skydiver

• Code herder

Reputation: 7192
• Posts: 24,369
• Joined: 05-May 12

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:08 PM

Don't you still have the warning about signed vs. unsigned comparisons?

And you don't handle the empty string. Try changing line 17 to:
```test = "";

```

and see what happens.

This post has been edited by Skydiver: 31 August 2012 - 08:09 PM

### #10 jgar

Reputation: 0
• Posts: 16
• Joined: 31-August 12

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:15 PM

Skydiver, on 31 August 2012 - 08:01 PM, said:

Change line 4 to:
```decltype(name.size()) x;

```

to get rid of the warning.

But beware that a logic bug persists on line 5. Notice what happens if size() returns 0?

You'll need to change the loop condition to use less than rather than less than or equal, and change the loop limit.

Thanks D.I.C LOVER;) ..... I think the function its right the way it is, the thing that I was missing was the
std:: in the main function... that was the reason why the program did not run.

Skydiver, on 31 August 2012 - 08:08 PM, said:

Don't you still have the warning about signed vs. unsigned comparisons?

And you don't handle the empty string. Try changing line 17 to:
```test = "";

```

and see what happens.

Thank you so much for wanting to help me..

### #11 jimblumberg

Reputation: 5782
• Posts: 17,685
• Joined: 25-December 09

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:15 PM

CTphpnwb, on 31 August 2012 - 09:56 PM, said:

It works for me.

Actually you should be getting a warning also. The std::string.size() function returns a size_t which is not an int. It is an unsigned type. You should also use this type in your for loop.
```// int x; // This line is not required.
for(size_t x = 0; x <= name.size()-1; x++)
```

@jgar
It is much easier if you stop using the <= operator and just use the < operator in your loop. Then you will have less chances of forgetting to subtract the 1.
``` for(size_t x = 0; x < name.size(); x++)
```

Jim

### #12 jgar

Reputation: 0
• Posts: 16
• Joined: 31-August 12

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:24 PM

jimblumberg, on 31 August 2012 - 08:15 PM, said:

CTphpnwb, on 31 August 2012 - 09:56 PM, said:

It works for me.

Actually you should be getting a warning also. The std::string.size() function returns a size_t which is not an int. It is an unsigned type. You should also use this type in your for loop.
```// int x; // This line is not required.
for(size_t x = 0; x <= name.size()-1; x++)
```

@jgar
It is much easier if you stop using the <= operator and just use the < operator in your loop. Then you will have less chances of forgetting to subtract the 1.
``` for(size_t x = 0; x < name.size(); x++)
```

Jim

It's a good idea:) thank you Jim.

### #13 jimblumberg

Reputation: 5782
• Posts: 17,685
• Joined: 25-December 09

## Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:50 PM

Skydiver, on 31 August 2012 - 10:01 PM, said:

Change line 4 to:
```decltype(name.size()) x;

```

to get rid of the warning.

Note this will only work if your compiler supports the current standard (C++11).

Jim

### #14 CTphpnwb

• D.I.C Lover

Reputation: 3826
• Posts: 13,945
• Joined: 08-August 08

## Re: convert a string into capital letter using a function

Posted 01 September 2012 - 04:14 AM

jimblumberg, on 31 August 2012 - 11:15 PM, said:

CTphpnwb, on 31 August 2012 - 09:56 PM, said:

It works for me.

Actually you should be getting a warning also. The std::string.size() function returns a size_t which is not an int. It is an unsigned type. You should also use this type in your for loop.
[/code]

I thought so too, and I had turned warnings up to Pedantic but didn't get one. It turns out I still had more warnings to turn on. I used Xcode 4.4.1 for this, but I haven't fully switched from XCode 3.3 yet.
Anyway, using unsigned long shows no issues with my higher warnings set:
```#include <iostream>
#include <string>

void capital(std::string &name)
{
unsigned long x;
for( x = 0; x <= name.size()-1; x++)

if((name.at(x) >= 'a') && (name.at(x) <= 'z'))

name.at(x) -= 32;
}

int main(int argc, const char * argv[])
{
std::string test;
test = "hello world!";
capital(test);
std::cout << test <<std::endl;
return 0;
}

```

### #15 jimblumberg

Reputation: 5782
• Posts: 17,685
• Joined: 25-December 09

## Re: convert a string into capital letter using a function

Posted 01 September 2012 - 07:11 AM

You should get used to using the proper type of variable, a size_t is not necessarily an unsigned long. The standard defines this type only to be an unsigned type large enough to hold any object, it is up to the implementation to define which type it actually is. Since std::string.size() returns a size_t you actually should use a size_t in your loop.
```	size_t x;
for( x = 0; x <= name.size()-1; x++)
```

Your implementation will typedef size_t to be the proper size for your architecture.

Jim