Function for a string

This is a discussion on Function for a string within the C++ Programming forums, part of the General Programming Boards category; I need to write a function that will take a string and an int and return the string from index ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    Question 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

  2. #2
    dra
    dra is offline
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    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.
    Last edited by dra; 05-23-2006 at 02:37 AM.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    50
    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: "
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,628
    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: "
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    2

    Thumbs up Thank you very much guys

    You've been very helpfull and very nice to put up with a noob .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM

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