char Name[10]="JOHN DOE", Newname[30]; char *p;Statement
- : p=strchr(Name,' '); Return a pointer p to the first blank in Name. The first letter of last name begins at address p+1.
![]()
Statement
- : *p=0; //replace the blank with the null character.
![]()
Statement
- : strcpy(Newname,p+1); //copy the last name to Newname.
![]()
Statement
- : strcat(Newname,", "); //add ',' add blank to Newname.
![]()
Statement
- : strcat(Newname,Name);// concatenate the first name.
![]()
Reverse a Name
#include <iostream.h> #include < string.h > //reverse first and last name and separate them. //With a comma. copy result to newName. void ReverseName(char *name, char *newName) { char *p; //search for first blank in name; replace with NULL char p=strchar(name,' '); *p=0; //copy last name to newName, append "," and // concatenate first name. strcpy(newName,p+1); strcat(newName,", "); strcat(newName,name); //replace Null character with original blank *p=' '; } void main(void) { char name[32], newName[32]; int i; // read and process 3 names for(i=0; i< 3; i++) { cin.getline(name,32,'\n'); ReverseName(name,newName); cout << "Reversed name: " << Newname << endl << endl; } } /* < Run of Program > Abraham Lincoln Reversed Name: Lincoln, Abraham Debbie Rogers Reversed Name: Rogers, Debbie Jim Brady Reversed Name: Brady, Jim */