Reverse Name Function

a function to reverse names with strings

Page 1 of 1

3 Replies - 9008 Views - Last Post: 21 March 2008 - 02:53 AM Rate Topic: -----

#1 Zerobu   User is offline

  • Black Hatter

Reputation: 13
  • View blog
  • Posts: 1,822
  • Joined: 14-January 08

Reverse Name Function

Post icon  Posted 20 March 2008 - 05:55 PM

I need help[ writing a function to reverse names from first name first status to last name first status.
Example:
John Doe would become
Doe, John.

This should be done using the string functions in the <string> library but i dont know where should i start can someone point me in the right direction
Is This A Good Question/Topic? 0
  • +

Replies To: Reverse Name Function

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Reverse Name Function

Posted 20 March 2008 - 06:00 PM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: :code:

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#3 letthecolorsrumble   User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: Reverse Name Function

Posted 20 March 2008 - 06:06 PM

You could split the string and then display them in the other order.

To split a string you might have to search the snippets section, there should be something about splitting a string.

Other than that, we can not help you much with you showing us what you have done so far.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#4 Sepanto   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 97
  • Joined: 20-March 08

Re: Reverse Name Function

Posted 21 March 2008 - 02:53 AM

I think it's kinda simple...
void reverse_str(char* str){
char *temp=strstr(str," ");
char * t1=new char [strlen(str)+1];
while (str!=" "){
t1++=str++;
}
strcat(t1,temp);
delete [] str;
delete []temp;
str=new char[strlen(temp)+1];
strcpy(str,t1);
delete [temp];
}


there's probably a better way, but i can't find it atm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1