i) Create a temporary storage area and move to the end in the original string.Then pick a word from the right by moving left and encountering a space.and store these words in new array with a space appended.
ii)Reverse the string and then apply the same logic for each reversed word as you did for reversing the whole string.(this method is better as it does not require extra memory of order(n)
Hi Sachitra,You need to give the string which is to be reversed as a command line argument.e.gif ur exe/binary name is "abc" thenif u type "abc Hello World" on the command prompt then u'll get "World Hello" as the outputcheersHemant
rajesh
Sep 26th, 2006
please send me the answer to make myself better in veifying my answer.
Ramana Babu
Dec 3rd, 2006
This is a simple program to reverse the string entered. The string will be reversed till enter key is pressed.
There are two ways to solve this problem......1. Make a linked list of all words that are in the string and then reverse the linked list.2. First reverse the string taking 'n' as delimeter and reverse it again taking ' ' space as argumentthanks n regards....rameshwar.....
Ravi
Feb 27th, 2007
Algorithm : 1. reverse the original striing 2. pick each word in the string and reverse each and you will get the result !
Alice McGregor
Mar 9th, 2007
I like my solution: chopping the original string. Only uses a single local variable as a pointer runner. This method isn't as creative as using separate arguments, but it does work with a wider range of test cases -- spaces at the beginning, end, and multiple spaces anywhere, all work as you would expect.#include #include int main(int *argc, char **argv) { char *p = argv[1]; /* Run through the array of characters, replacing spaces with NULLs. */ /* This effectively splits the string into substrings. */ for ( ; *p != 0; p++ ) if ( *p == ' ' ) *p = 0; /* Loop backwards through the allocated memory. */ /* Output any strings found, handling the first word of the argument. */ for ( p-- ; p >= argv[1]-1; p-- ) if ( *p == 0 || p < argv[1]) printf("%s ", p+1); printf("n");}
anshuman sinha
Mar 18th, 2007
see the c# code: static void Main(string[] args) { string string1; int i = 0; string[] string3; Console.WriteLine("Input the string"); string1 = Console.ReadLine(); string1 = string1.Trim(); char[] seperator = { ' ' }; string3 = string1.Split(seperator);
foreach(String string2 in string3) {
i = i + 1; }
for (int k = i-1; k >= 0; k--) { Console.Write("{0} ",string3[k]); } Console.ReadKey(); }
ILYAS
Mar 29th, 2007
C# code for this problem
class Program { static void Main(string[] args) {
String []result; Console.Write("Please enter the string "); String inputString = Console.ReadLine();
result= inputString.Split (' ') ; int siz = result .Length ; for (int i = siz-1; i >=0; i--) {
Console.Write(result [i]); Console.Write(" ");
} Console.Read(); } }
mo
Apr 2nd, 2007
template<typename T> void reverseArr(T* arr, int len) { T tmp; int length = len-1; for(int i=0; i<len/2; i++) { tmp = arr[i]; arr[i] = arr[length-i]; arr[length-i] = tmp; } }
char* reverseString(char* s) { int length = strlen(s); reverseArr<char>(s,length); cout << s<<endl; int i = 0, j = -1; for(;;) { while(j < length && s[++j] != ' ' ); reverseArr<char>(s+i,j - i ); if(j >= length) break; i = j+1; } return s; }
One thing you can do is save different strings as the nodes of the link list. Meaning for eg. Jayanth is a programmer. First node jayanth. Second is ..and so on. Then we just reverse the link list.
Sridhar
Jun 6th, 2007
I think using strstr( ) should make the program more readable. I did not like playing with pointers (see below), but had no choice.
main() { char *srcStr = "This is the string to be reversed";
char destStr[100];
int breakLoop = 0;
char *p1, *p2; // traverse srcStr (front to back) char *d = destStr + strlen (srcStr); // traverse dest str (back to front)
How do display the output as reverse of input, like input is "This is a hat" and the output should be "hat a is This"?
Profile Answers by monu2784 Questions by monu2784
Questions by monu2784