4 Replies - 1517 Views - Last Post: 11 August 2009 - 12:16 PM Rate Topic: -----

#1 Emon_MQ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 06-August 09

using concatenation of string inside loop

Post icon  Posted 11 August 2009 - 10:41 AM

hi i want to go through each of the charecter of a string without using str[loop counter].
like
for(int i=o;i<=str.length;i++)
str[i];

insted
for(int i=0;i<=str.length;i++)
str=str[1]+str[2]+str[3]....str[str.length();

any suggestion??
Is This A Good Question/Topic? 0
  • +

Replies To: using concatenation of string inside loop

#2 redhotfire0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: using concatenation of string inside loop

Posted 11 August 2009 - 11:15 AM

View PostEmon_MQ, on 11 Aug, 2009 - 09:41 AM, said:

hi i want to go through each of the charecter of a string without using str[loop counter].
like
for(int i=o;i<=str.length;i++)
str[i];

insted
for(int i=0;i<=str.length;i++)
str=str[1]+str[2]+str[3]....str[str.length();

any suggestion??

str[i] = str[1]+str[2]+str[3]

Do you mean the elements of string, or multiple different strings?
Was This Post Helpful? 0
  • +
  • -

#3 Emon_MQ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 06-August 09

Re: using concatenation of string inside loop

Posted 11 August 2009 - 11:36 AM

No actually its the combination of all the charecters of string.Actually I want to write a function which returns the reverse of an input string.

string reverseOfgiven(string str)
{
for(int i=string.length();i<=string.length();i--)
str=str[1]+str[2] //.up2 str[str.length()]

return str;
}
Was This Post Helpful? 0
  • +
  • -

#4 bimbolena  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 50
  • Joined: 09-August 09

Re: using concatenation of string inside loop

Posted 11 August 2009 - 11:47 AM

View PostEmon_MQ, on 11 Aug, 2009 - 10:36 AM, said:

No actually its the combination of all the charecters of string.Actually I want to write a function which returns the reverse of an input string.

string reverseOfgiven(string str)
{
for(int i=string.length();i<=string.length();i--)
str=str[1]+str[2] //.up2 str[str.length()]

return str;
}


Try something like this:

string reverseOfgiven(string str)
 {
	string str2=str; // Or just create an empty string of the same length as str
	for(int i=0; i<string.length(); i++) {
		 str2[i]=str[string.length()-1-i];
	}
	return str2;
 }


Was This Post Helpful? 1
  • +
  • -

#5 Emon_MQ  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 06-August 09

Re: using concatenation of string inside loop

Posted 11 August 2009 - 12:16 PM

Thanks it works!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1