12 Replies - 8224 Views - Last Post: 11 May 2010 - 02:45 AM Rate Topic: -----

#1 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Convert string to uppercase without toupper

Posted 09 May 2010 - 11:27 PM

Hey guys,
Is there a way to convert a string to uppercase (without using toupper) by using standard string functions and without [] notation?

bool isPal(string& str)
{
	string::size_type pos;
	while ((pos = str.find_first_of("`~!@#$%^&*()-_=+{}|[]\\:;\"\'<>,.?/\t\r\n")) != string::npos)
	{
		str.erase(pos,1);
	}
	while ((pos = str.find_first_of(" ")) != string::npos)
	{
		str.erase(pos,1);
	}
	while ((pos = str.find_first_of("abcdefghijklmnopqrstuvwxyz")) != string::npos)
	{
		str[pos] = toupper(str[pos]); // any other way to convert the string to uppercase here?
	}	
	string rev(str.rbegin(), str.rend()); 
    return (rev == str);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Convert string to uppercase without toupper

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Convert string to uppercase without toupper

Posted 09 May 2010 - 11:34 PM

No. (Well, maybe, I'm not sure. But it would be some very bad code.)

This post has been edited by Oler1s: 09 May 2010 - 11:35 PM

Was This Post Helpful? 0
  • +
  • -

#3 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: Convert string to uppercase without toupper

Posted 09 May 2010 - 11:40 PM

you could subtract 32 from each char.
'a'-32 = 'A'


but I do not see what you`d gain :).

This post has been edited by muballitmitte: 09 May 2010 - 11:41 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jahmbo  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 35
  • Joined: 17-March 10

Re: Convert string to uppercase without toupper

Posted 10 May 2010 - 03:41 AM

View Postmuballitmitte, on 09 May 2010 - 10:40 PM, said:

you could subtract 32 from each char.
'a'-32 = 'A'


but I do not see what you`d gain :).


Yeah, that's pretty much what I was thinking, do a sanity check to see if the ascii value of the char is between the numbers for a to z (which would approximate the islower function in cctype.h) and if so subtract 32 to get A to Z (which would approximate the toupper function in cctype.h).

This post has been edited by Jahmbo: 10 May 2010 - 04:18 AM

Was This Post Helpful? 0
  • +
  • -

#5 cyfuzhi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 27-February 10

Re: Convert string to uppercase without toupper

Posted 10 May 2010 - 05:56 PM

Is there a way to use only string functions to find only the lower case letters and use another string function to swap/replace the letter without using [] and instead using a string dot operator?
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Convert string to uppercase without toupper

Posted 10 May 2010 - 06:03 PM

you can have a look at the string class and decide which functions fits most to your program
http://www.cplusplus.../string/string/

This post has been edited by japanir: 10 May 2010 - 06:03 PM

Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Convert string to uppercase without toupper

Posted 10 May 2010 - 06:34 PM

Yes, it can be done. :)

bool isPal(string& str)
{
   // pointer to string
   char *cp = (char *)str.c_str();
...
      //str[pos] = toupper(str[pos]); // any other way to convert the string to uppercase here?
      *(cp + pos) -= 32;
...


This post has been edited by n8wxs: 10 May 2010 - 06:35 PM

Was This Post Helpful? 1
  • +
  • -

#8 Guest_c.user*


Reputation:

Re: Convert string to uppercase without toupper

Posted 10 May 2010 - 07:22 PM

try this
struct ltrs {
    char small[27];
    char caps[27];
};

...

    struct ltrs b = {
        "abcdefghijklmnopqrstuvwxyz",
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    };

    char ch = 'a';

    p = strchr(b.small, ch);

    if (p != NULL)
        ch = b.caps[p-b.small];

    ...



in a function
this is for C, but you can do the same thing for C++ (by strings)

This post has been edited by c.user: 10 May 2010 - 07:25 PM

Was This Post Helpful? 1

#9 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,559
  • Joined: 09-June 09

Re: Convert string to uppercase without toupper

Posted 10 May 2010 - 07:28 PM

	std::string str = "ThIs iS A TeSt";
	for(int i=0; i<str.length(); i++)
		if(str.at(i)>='a' && str.at(i)<= 'z')
			str.at(i) = str.at(i) - 'a' + 'A';
	cout<<str<<endl;


This post has been edited by ImaSexy: 10 May 2010 - 07:37 PM

Was This Post Helpful? 1
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Convert string to uppercase without toupper

Posted 10 May 2010 - 07:34 PM

View PostImaSexy, on 10 May 2010 - 10:28 PM, said:

std::string str = "ThIs iS A TeSt";
for(int i=0; i<str.length(); i++)
	if(str.at(i) != toupper(str.at(i)))
		str.at(i) = toupper(str.at(i));
	
cout<<str<<endl;



Way to convert a string to uppercase without toupper!! :clap:
Was This Post Helpful? 1
  • +
  • -

#11 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,559
  • Joined: 09-June 09

Re: Convert string to uppercase without toupper

Posted 10 May 2010 - 07:42 PM

ha i fixed it :oops:
Was This Post Helpful? 1
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Convert string to uppercase without toupper

Posted 10 May 2010 - 07:51 PM

Much better.

I wonder ... what is the purpose of this fascinating :rolleyes2: exercise?
Was This Post Helpful? 1
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Convert string to uppercase without toupper

Posted 11 May 2010 - 02:45 AM

View Postr.stiltskin, on 11 May 2010 - 11:51 AM, said:

I wonder ... what is the purpose of this fascinating :rolleyes2: exercise?


Homework exercise surely?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1