Thread: Function to Reverse a string

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Illinois
    Posts
    8

    Cool Function to Reverse a string

    Hey guys im looking for some more insite on another problem my teacher gave me. I was tasked with creating a function that reverses a string. This is what ive come up with and it worked as a stand alone program but once i converted it to a function I keep getting an error when I compile it saying.

    Undefined symbols for architecture x86_64:
    "_main", referenced from:
    start in crt1.10.6.o
    ld: symbol(s) not found for architecture x86_64
    collect2: ld returned 1 exit status


    Code:
    string Reverse(string original)
    {
    	//decare and assign variables
    	int strlen=original.length();
    	string s=original, output="";
    	
    	//Loop to reverse letters. sloppy but it works.
    	for(int i=0,ii=(strlen-1); i<=strlen; i)
    	{
    		output[ii]=original[i];
    		i++;
    		ii--;
    	}
    	return(output);
    }
    
    
    Tested further and the error is somewhere else in the code. Disregard
    Any help is greatly appreciated. and I apologize for the sloppy coding I was in a bit of a rush. Thank you as always.
    Last edited by drumerboy3841; 09-13-2011 at 04:35 PM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Um. Where is your main() function? A program is not an executable program without a main() entry point.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Illinois
    Posts
    8
    I just pulled the function itself because I thought it was the problem. Here is the full program

    headerfile
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    void FibSeq();//Gives the fibonacci sequence until 30000
    void Reverse(string, string);//Takes a String and reverses it into another
    string Reverse(string);// Takes a string and returns the reverse
     
    Implementation File
    
    #include<iostream>
    #include<string>
    #include"HM2.h"
    
    using namespace std;
    
    void FibSeq()
    {
    	//Declaration
    	unsigned int fib1=0, fib2=1, fib3=0;
    	
    	//Loop
    	while(fib3<30000)
    	{
    		cout<<fib3<<endl;
    		fib3=fib1+fib2;
    		fib1=fib2;
    		fib2=fib3;
    	}
    }
    
    string Reverse(string original)
    {
    	//decare and assign variables
    	int strlen=original.length();
    	string s=original, output="";
    
    	
    	//Loop to swap letters. sloppy but it works.
    	for(int i=0,ii=(strlen-1); i<=strlen; i)
    	{
    		output[ii]=original[i];
    		i++;
    		ii--;
    	}
    	return(output);
    }
    
    Quick Test Program
    
    #include<string>
    #include<iostream>
    #include"HM2.h"
    #include"HM2.cpp"
    
    using namespace std;
    
    int main()
    {
    	FibSeq();
    	Reverse("steve");
    }
    The Fibonacci function prints fine, but the steve function does nothing. Any idea whats up? Thank you

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Your reverse function returns a string however you do not do anything with that and your function doesn't print anything out. So what exactly were you expecting it to do(besides crash that is)?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Illinois
    Posts
    8
    Hm even if I change it to cout<<Reverse("steve")<<endl; in the main it still prints nothing. But Ill keep working on it

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by drumerboy3841 View Post
    Hm even if I change it to cout<<Reverse("steve")<<endl; in the main it still prints nothing. But Ill keep working on it
    I believe your function is crashing, and that is why you aren't seeing anything. As for reversing a string, you may want to try something along the lines of:
    Code:
    #include <iostream>
    #include <string>
    
    int main(void){
    
    	std::string input, output;
    
    	std::cout<<"Enter string to reverse:";
    	std::cin>>input;
    
    	for(int i = input.size()-1;i>=0;i--){
    		output+=input[i];
    	}
    
    	std::cout << "Reversed string: " << output;
    	
    	return(0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    std::string original("Steve");
    std::string reversed(original.rbegin(),original.rend());
    
    std::cout << "Original string: " << original << std::endl;
    std::cout << "Reversed string: " << reversed << std::endl;
    Done!

    If you want to put it into a function:
    Code:
    std::string ReverseString(const std::string& original)
    {
      return std::string(original.rbegin(),original.rend());
    }
    Almost doesn't even seem to deserve its own function as short as it is.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Sep 2011
    Location
    Illinois
    Posts
    8
    I agree haha Well thanks again guys. This should work for me. Hopefully I can learn enough to start helping people I feel like a bum :/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reverse a string without string.h library
    By antros48 in forum C Programming
    Replies: 6
    Last Post: 09-10-2011, 06:01 PM
  2. reverse string without using strrev() function
    By revolution3396 in forum C Programming
    Replies: 3
    Last Post: 11-27-2008, 02:34 AM
  3. C function to reverse a given String!!
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 07-18-2002, 10:35 PM
  4. is there a function to reverse a string ?
    By Spectrum48k in forum C Programming
    Replies: 1
    Last Post: 07-18-2002, 03:25 PM
  5. A function to reverse a string and return it...
    By Nutshell in forum C Programming
    Replies: 18
    Last Post: 01-11-2002, 11:19 PM