English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

write a program that takes a line at a time and reverses the words of the lines.

ex. input: i am confused
output: confused am i

the data should contain one blank between each word.

any idea on how to solve this problem?
please give some ideas... how to solve this in c programming language...?

2007-02-25 16:12:07 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

the first one who answered this is the problem:

input: i am hungry
output: yrgnuh ma i

but it is supposed to be:
output: hungry am i

2007-02-25 16:39:04 · update #1

5 answers

ur program is bit difficult and is not so easy as it seems. what u got as first answer is incorrect since it will print only reverse charcater of ur answer. i.e. this is me gives em si siht which is not what u wanted. i have tried to write a program but the program has some bug and it is not giving what i wanted. i have done the following
1. count the no of blank spaces in string. this will give u the number of words in sentece say no_blank.
2. then create a array that stores the location of blank spaces in the sentence int array[].
3. using that array u then copy the word between two blank spaces from back.

// !! RAM !!

#include"stdio.h"
#include"string.h"
char* returnreverse( char a[] )
{
int no=1;
for( int i=0;i {
if( a[i] == ' ')
no++;
}
char *words = new char [no];
int r=0;
for( int j=0;j {

if( a[j] == ' ' )
{
words[r] = j;
r++;
}
}
words[r]= strlen(a-1);

char *rev= new char[strlen(
a)];
int m=0;
for( int h=r;h>= 0;h--)
{

// copy the words from a[words[h-1] ] to a[words[h-1]] in rev
for(int s= words[h-1];s {
rev[m] = a[s];
m++;
}
rev[m]=' ';
m++;
}
return rev;
}


main()
{
char s[100];
printf("\n\n\tEnter a string : ");
gets(s);
printf("ur reverse string is");
printf( returnreverse(s) );
printf("\n");
return 0;
}
.

2007-02-25 18:31:54 · answer #1 · answered by Anonymous · 0 0

1

2017-01-19 14:40:18 · answer #2 · answered by ? 2 · 0 0

first you should save every particular character of that string in another array meaning you have to declare a new array and put the first character in the last index and so on.... you should use some string methods to be able to get the length of the string. I remembered that It might be "string.length" or something like that.

2007-02-26 01:30:07 · answer #3 · answered by andrew 1 · 0 0

write a module to opposite a given string "i'm puzzled" could desire to grow to be "desufnoc ma i" Then split the string on areas and pass each and each observe returned to the comparable module. "desufnoc ma i" could desire to grow to be "puzzled am i" Thats one way that could desire to artwork

2016-10-02 00:09:09 · answer #4 · answered by ? 4 · 0 0

#include
#include
#include



int main() {

char s[1000];
int i;
int len=0;

while (1) {

printf("\n\n\tEnter a string : ");
gets(s);

printf("\n");

for(i=0;s[i];i++) {
printf("%c",s[i]);
}
len=strlen(s);
printf("\n\n");
for(i=len-1; s[i], i>=0; i--)
printf("%c",s[i]);
}
return 0;
}

2007-02-25 16:18:30 · answer #5 · answered by iyiogrenci 6 · 0 0

fedest.com, questions and answers