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...?
C programming problem?
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%26lt;strlen(a);i++)
{
if( a[i] == ' ')
no++;
}
char *words = new char [no];
int r=0;
for( int j=0;j%26lt;strlen(a);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%26gt;= 0;h--)
{
// copy the words from a[words[h-1] ] to a[words[h-1]] in rev
for(int s= words[h-1];s%26lt;words[h];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;
}
.
Reply:#include %26lt;string.h%26gt;
#include %26lt;stdio.h%26gt;
#include %26lt;stdlib.h%26gt;
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%26gt;=0; i--)
printf("%c",s[i]);
}
return 0;
}
Reply: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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment