View all questions by monu2784
Answered On : Jul 3rd, 2006
There are two ways:
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)
#include <stdio.h>
void main(int argc,char** argv[])
{
int i;
for(i=argc;i>1;i--)
{
printf("%s ",argv[i-1]);
}
printf("n");
}
cheers
hemant
Answered On : Sep 14th, 2006
respected sir,
i run ur program...
but i am to get the ans ...
please give advice how to run the program correctly..
thank you sir
urs truly
suchitra.A
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
Answered On : Sep 26th, 2006
please send me the answer to make myself better in veifying my answer.
Answered On : Dec 3rd, 2006
This is a simple program to reverse the string entered. The string will be reversed till enter key is pressed.
void main()
{
char c;
if((c = getchar()) != 'n')
{
main();
}
putchar(c);
}
Answered On : Dec 13th, 2006
View all questions by vipin gupta View all answers by vipin gupta
Here is a recursive version:
void reverse(char *str)
{
static int first_flag=1; // to take care of first word
if(strlen(str) == 0)
return;
char *word_boundary = NULL;
if(*str == ' ')
word_boundary = str + 1;
else if(first_flag == 1)
{
word_boundary = str;
first_flag = 0;
}
reverse(++str);
if(world_boundary != NULL)
while(world_boundary != ' ' || world_boundary != 'o')
printf("%c",*world_boundary++);
}
thanx
jus push the string into the stack and pop the stack once.
Answered On : Feb 12th, 2007
View all questions by rameshwar83 View all answers by rameshwar83
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.....
Answered On : 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 !
Answered On : 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
Answered On : 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();
}
Answered On : 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();
}
}
Answered On : Apr 2nd, 2007
template
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
cout << s<<endl;
int i = 0, j = -1;
for(;;)
{
while(j < length && s[++j] != ' ' );
reverseArr
if(j >= length) break;
i = j+1;
}
return s;
}
Answered On : Apr 4th, 2007
View all questions by dhanasweet View all answers by dhanasweet
hai ramana,
i executed ur program it is not working,
the program reverse the character,
Answered On : Apr 5th, 2007
hai ,
the program s
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20];
scanf("%s",a);
if(strcmp(a,"n")==0)
return;
else
main();
printf("%s",a);
}
at the end of ur input u have to give n(space n)
Answered On : May 24th, 2007
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.
Answered On : 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)
p1 = p2 = srcStr;
*d-- = '
