convert a string into capital letter using a function

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 4972 Views - Last Post: 01 September 2012 - 12:19 PM Rate Topic: -----

#1 jgar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • D.I.C Lover
  • member icon

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

Re: convert a string into capital letter using a function

Posted 31 August 2012 - 07:32 PM

What have you tried?
Was This Post Helpful? 0
  • +
  • -

#3 jgar   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5782
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#6 jgar   User is offline

  • New D.I.C Head

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

Re: convert a string into capital letter using a function

Posted 31 August 2012 - 07:57 PM

View Postjimblumberg, 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.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7192
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#8 jgar   User is offline

  • New D.I.C Head

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

Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:03 PM

View PostCTphpnwb, 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.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7192
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#10 jgar   User is offline

  • New D.I.C Head

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

Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:15 PM

View PostSkydiver, 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.

View PostSkydiver, 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..:)
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg   User is offline

  • member icon

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

Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:15 PM

View PostCTphpnwb, 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
Was This Post Helpful? 0
  • +
  • -

#12 jgar   User is offline

  • New D.I.C Head

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

Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:24 PM

View Postjimblumberg, on 31 August 2012 - 08:15 PM, said:

View PostCTphpnwb, 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.
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is offline

  • member icon

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

Re: convert a string into capital letter using a function

Posted 31 August 2012 - 08:50 PM

View PostSkydiver, 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
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

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

Re: convert a string into capital letter using a function

Posted 01 September 2012 - 04:14 AM

View Postjimblumberg, on 31 August 2012 - 11:15 PM, said:

View PostCTphpnwb, 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;
}


Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg   User is offline

  • member icon

Reputation: 5782
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2