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??
using concatenation of string inside loop
Page 1 of 14 Replies - 1517 Views - Last Post: 11 August 2009 - 12:16 PM
Replies To: using concatenation of string inside loop
#2
Re: using concatenation of string inside loop
Posted 11 August 2009 - 11:15 AM
Emon_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??
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?
#3
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;
}
string reverseOfgiven(string str)
{
for(int i=string.length();i<=string.length();i--)
str=str[1]+str[2] //.up2 str[str.length()]
return str;
}
#4
Re: using concatenation of string inside loop
Posted 11 August 2009 - 11:47 AM
Emon_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;
}
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;
}
#5
Re: using concatenation of string inside loop
Posted 11 August 2009 - 12:16 PM
Thanks it works!!!
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|