-
Function for a string
I need to write a function that will take a string and an int and return the string from index int onward.
for ex
Code:
Line = "Enter a line: ";
WriteString(RetStr(Line,4 ));
//Should return er a line:
//and below is the function
Code:
AnsiString RetStr(AnsiString Str, int Start)
{ int ill;
ill=1;
char temp;
while (Start!=Length(Str))
{
temp=Str[Start];
Str[ill]=temp;
ill=ill+1;
Start=Start+1;
}
return Str;
}
I've tried it in several combinations like trying to use a for loop but
Str[ill]=Str[Start+1] gives some errors that i don't know what to make of them.
Please help...
Any pointers will be greatly appreciated
-
trying using an iterator.
Code:
string s = "Hey there";
string::iterator a = s.begin(); b = s.end();
a = a+1;
string d = string( a, b );
cout << d;
the output should be "ey there" or something like that.
sorry i'm pretty tired, don't even know if that code's valid haha.
-
Something like this perhaps?
Code:
#include <iostream>
#include <string>
using namespace std;
string RetStr(string s, int x)
{
string p;
for ( int i = 0; i < s.length(); i++ )
{
if ( i >= x - 1 )
{
p += s[i];
}
}
return p;
}
int main ()
{
string Line = "Enter a line: ";
cout << RetStr(Line, 4) << endl; // returns "er a line: "
}
-
zackywacky is apparently using AnsiString, which I am not familiar with.
That said, the various implementations of RetStr() with std::string that have been suggested might as well be replaced with the member function substr().
For example, dra's example can be rewritten as:
Code:
std::string s = "Hey there";
std::cout << s.substr(1) << std::endl;
Sfel's example would be:
Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string Line = "Enter a line: ";
cout << Line.substr(4) << endl; // returns "r a line: "
}
-
Thank you very much guys
You've been very helpfull and very nice to put up with a noob .