string conversion

how to convert a string variable into capital in c++ code

Page 1 of 1

2 Replies - 5797 Views - Last Post: 02 April 2007 - 05:50 AM Rate Topic: -----

#1 ZORO   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-April 07

string conversion

Posted 02 April 2007 - 12:00 AM

how to convert a string variable into capital in c++ code
Is This A Good Question/Topic? 0
  • +

Replies To: string conversion

#2 v0id   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 22-November 06

Re: string conversion

Posted 02 April 2007 - 12:15 AM

Hi ZORO,

I don't know if there's such function, but you can make one by yourself, very quickly. I've just maked an example;
#include <iostream>
#include <string>

void makeCapital(std::string &str)
{
	for(int x = 0; x <= str.size()-1; x++)
		if((str.at(x) >= 'a') && (str.at(x) <= 'z'))
			str.at(x) -= 32;
}

int main()
{
	std::string myStr = "Hello, World!";
	
	makeCapital(myStr);
	std::cout << myStr << std::endl;
	
	return 0;
}


The only thing to actually know, is the algorithm (if I can say so).
Each small letter have to be subtracted by a ' ', or 32 (ASCII). That's because there's 32 from the big letter, for example 'A', to it's smaller letter, 'a'.
Was This Post Helpful? 1

#3 Amadeus   User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: string conversion

Posted 02 April 2007 - 05:50 AM

http://www.dreaminco...e/snippet39.htm

or using the STL

std::transform (s.begin(),s.end(), s.begin(), toupper); 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1