C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-27-2010, 08:36 PM   #1
Registered User
 
Join Date: Sep 2009
Posts: 68
Using .find and .replace with strings

Ok here is my homework assignment... I have it working for some instances but not for others.

Here is the code that does the conversion..

Essentially i have a string like... C++ is awesome!

it must convert C++ to java.
then it converts all letters to Uppercase

I have it working with there is one instance of C++, however if there is none or more than one it will not work proper. Im figured i need to do some kind of loop that goes through checking for C++ and changing it if it finds it .... not sure how exactly to do that

Code:
modified = original;
pos = original.find("C++");
modified.replace(pos,3,"java");

upperStr = modified;

int i = 0;

while (i<modified.length())
{
	if (modified[i] == ' ' || modified[i] == ',' || modified[i] == '.')
	{
		upperStr[i] = modified[i];
	}
	if (modified[i] >= 65 && modified[i] <= 90)
	{
		upperStr[i] = modified[i] + 32;
	}
	if (modified[i] >= 97 && modified[i] <= 122)
	{
		upperStr[i] = modified[i] - 32;
	}
i++;

}
bleuz is offline   Reply With Quote
Old 01-27-2010, 08:56 PM   #2
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,425
Code:
	if (modified[i] >= 65 && modified[i] <= 90)
	{
		upperStr[i] = modified[i] + 32;
	}
	if (modified[i] >= 97 && modified[i] <= 122)
	{
		upperStr[i] = modified[i] - 32;
	}
This is a very bad way to convert to upper case. Try running this on a non-ASCII machine and you'll be converting your string to garbage (not to mention the magic numbers you're using). There are toupper() and tolower() functions that do this portably.
__________________
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
cpjust is offline   Reply With Quote
Old 01-27-2010, 09:06 PM   #3
Registered User
 
Join Date: Sep 2009
Posts: 68
Yes i agree with you

however we are not able to use the header file needed for those functions
bleuz is offline   Reply With Quote
Old 01-27-2010, 09:37 PM   #4
Registered User
 
Join Date: Sep 2009
Posts: 68
anymore advice would be appreciated
bleuz is offline   Reply With Quote
Old 01-27-2010, 11:46 PM   #5
Registered User
 
Join Date: Jan 2005
Posts: 7,278
Instead of using numbers like 65 and 97, use the character literals like 'A' and 'Z'. It works the same, and it is much clearer to the person reading the code.

As to your find/replace question, you need to use a loop. Keep calling find and checking the return value. If find returns a valid position, use it to perform the replace. Then use that position as the starting point for your next call to find so that you don't keep looking over the same text over and over. When find doesn't find anything, end your loop. I believe find returns string::npos (which is basically -1) when it can't find the text, but you should verify that first.
Daved is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 12:17 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22