11 Replies - 4418 Views - Last Post: 27 May 2010 - 04:27 AM Rate Topic: -----

#1 Guest_Sammy H*


Reputation:

String manipulation

Posted 25 May 2010 - 05:53 PM

Just trying to change any upper case chars in the inputted strings, to lower case. is this the best way to go about it? i get an error with the line 'c = word.substr(i, 1);'
main.cpp:43: error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char' in assignment

void casechange(string word)
{
    char c;
    for(int i=0; i < word.size(); i++)
        {
            c = word.substr(i, 1);
            if(isupper(c)
               c = tolower(c);
            }
}



Is This A Good Question/Topic? 0

Replies To: String manipulation

#2 n8wxs  Icon User is offline

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

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

Re: String manipulation

Posted 25 May 2010 - 05:59 PM

That's because substr() returns a string, not a character.

Use string::operator[] or string::at

This post has been edited by n8wxs: 25 May 2010 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: String manipulation

Posted 25 May 2010 - 06:03 PM

substr returns another string object, not a char.

You could use the [i]str.at(index)[/il] function to return a char, or the [] operator
http://www.cplusplus...ring/string/at/

Here, you have unbalanced parentheses:
if(isupper(c) //missing closing )



And in order for the change to actually take effect, you need to assign the value back into the string.

You'll also probably want to change the return type to string instead of void
Was This Post Helpful? 0
  • +
  • -

#4 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 123
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: String manipulation

Posted 26 May 2010 - 03:03 AM

This actually drove me to some research, I had not done any string manipulation in awhile. With a little reading and the help of the following snippet,
http://www.dreaminco...e/snippet39.htm (Thanks Amadeus)

I was able to draw the following conclusion. I used a class for my example, but it should be simple enough for you to figure out how to incorporate this into a simple function.

#include<iostream>
#include<string>


using namespace std;


class ChangeCase
{
public:

	ChangeCase(){};
	~ChangeCase(){};

	string caseFunction(string inputString);

};


string ChangeCase::caseFunction(string inputString)
{
	for(unsigned int i = 0; i < inputString.length(); i++)
	{
		if(isupper(inputString.at(i)))
		{	
			inputString[i] = tolower(inputString[i]);

		}
	}
	
	return inputString;
}
	

int main()
{
	string passString = "HELLO";

	cout<<"Original string:   "<<passString<<endl;
	
	ChangeCase obj;

	cout<<"Lower Case string is:   ";
	cout<<obj.caseFunction(passString)<<endl;

	cout<<endl;

	cin.get();
	
return 0;
}


This post has been edited by alias120: 26 May 2010 - 03:04 AM

Was This Post Helpful? 0
  • +
  • -

#5 animecrazy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-May 10

Re: String manipulation

Posted 26 May 2010 - 04:45 AM

question...

are tolower() and isupper() built in functions....?
if they are then what exactly is their function?
Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: String manipulation

Posted 26 May 2010 - 04:48 AM

Yes they are. tolower() converts an uppercase character to lowercase. isupper() checks if a character is an uppercase letter.
Was This Post Helpful? 0
  • +
  • -

#7 animecrazy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-May 10

Re: String manipulation

Posted 26 May 2010 - 05:05 AM

View Postsarmanu, on 26 May 2010 - 03:48 AM, said:

Yes they are. tolower() converts an uppercase character to lowercase. isupper() checks if a character is an uppercase letter.



thanks ....and what is the meaning of this "(inputString.at(i))" whats the ".at" part for?
Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: String manipulation

Posted 26 May 2010 - 05:07 AM

It accesses the element at position "i". It's the same thing as inputString[i] (operator[] of std::string class), but .at(pos) function performs a range check too, and it throws an out of range exception in case the parameter "pos" is not a valid position in the string.
Was This Post Helpful? 1
  • +
  • -

#9 animecrazy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 22-May 10

Re: String manipulation

Posted 26 May 2010 - 05:15 AM

View Postsarmanu, on 26 May 2010 - 04:07 AM, said:

It accesses the element at position "i". It's the same thing as inputString[i] (operator[] of std::string class), but .at(pos) function performs a range check too, and it throws an out of range exception in case the parameter "pos" is not a valid position in the string.


hmm.. so one can also use it to set characters at certain positions in the string..... and how is the range determined.... is it determined automaically when we input something in the string?
Was This Post Helpful? 0
  • +
  • -

#10 Radical Edward  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 24-May 10

Re: String manipulation

Posted 26 May 2010 - 05:22 AM

isupper() is not needed to do the conversion. If the character does not have a lower case variant, tolower returns the argument unchanged:
#include <cctype>
#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string s;

    cout << "Enter a string: ";

    if (getline(cin, s))
    {
        for (string::size_type x = 0; x < s.size(); ++x)
        {
            s[x] = tolower(s[x]);
        }

        cout << "Lower case: " << s <<'\n';
    }
}


Quote

what is the meaning of this "(inputString.at(i))" whats the ".at" part for?

std::string has two ways of indexing. The [] operator works just like an array and does not do any range checking. The at() method does the same thing as the [] operator but if the index is out of range for the string, an exception is thrown. Here is an example of what the difference might look like:
reference string::operator[](size_type index)
{
    return _internal[index];
}

reference string::at(size_type index)
{
    if (index >= size())
        throw out_of_range("Index out of range");

    return _internal[index];
}


If the index is taken from a loop from 0 to the length of the string then the at() method adds unnecessary overhead for checking the range. Ed recommends using the [] operator in that case because there is no doubt that the index is in range.
Was This Post Helpful? 0
  • +
  • -

#11 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: String manipulation

Posted 26 May 2010 - 10:07 AM

Just a note: using an if to determine if a char is upper case and then calling tolower like this:
            if(isupper(c))
               c = tolower(c);
            }


isnot really necessary. tolower will only change the char if it is an upper case letter... so you could just make that into:
c = tolower©;

(as Radical Edward's example has)
Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

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

Re: String manipulation

Posted 27 May 2010 - 04:27 AM

View Postanimecrazy, on 26 May 2010 - 09:15 PM, said:

hmm.. so one can also use it to set characters at certain positions in the string..... and how is the range determined.... is it determined automaically when we input something in the string?


Have a read here for all you want to know:
http://www.cplusplus.../string/string/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1