Given an array of characters which form a sentence of
words, give an efficient algorithm to reverse the order of
the words (not characters) in it.
Answers were Sorted based on User's Feedback
Answer / patrck
//check this program....
#include <string.h>
#include <stdio.h>
main(){
char a[100],b[100];
int i,j=0,c=0,k,cnt=0;
printf("enter the sentence\n");
for(i=0;i>=0;i++){
a[i]=getchar();
if(a[i]=='\n')break;
}
for(i=0;a[i]!='\n';i++)
cnt++;
for(i=0;i<(cnt+1);i++)
b[i]=' ';
for(i=0;i<(cnt+1);i++){
c++;
if((a[i]==' ')||(a[i]=='\n')){
c=c-1;
for(j=(cnt-i),k=(i-c);c>0;k++,j++,c--){
b[j]=a[k];
}c=0;
}
}
printf("result: ");
for(i=0;i<cnt+1;i++)
putchar(b[i]); printf("\n");
}
Is This Answer Correct ? | 10 Yes | 1 No |
Answer / manish
int i, j, k;
int t = 0;
char[] c = "manish singhal is here".ToCharArray();
j = c.Length;
char[] b=new char[c.Length];
for (i = c.Length - 1; i >= 0; i--)
if ((i > 0 && c[i - 1] == ' ') || i == 0)
{
for (k = i; k < j; k++)
b[t++] = c[k];
if (i > 0)
b[t++] = c[--i];
j = i;
}
Console.WriteLine(b);
Console.ReadLine();
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / vijay
#include<stdio.h>
main()
{
int i=0,j=0,start=0,end=0,len,w_len;
char temp;
char str[]="Papa Kehte HAIN bada naam karega";
printf("Before reversing the words string is %s \n",str);
len=strlen(str);
for(i=0,j=len-1;i<j;i++,j--)
{
temp=str[j];
str[j]=str[i];
str[i]=temp;
}
for(i=0,j=0;str[i]!=0;)
{
i=j;
for(;str[i]!=' '&& i<len;)
i++;
w_len=(i-j)-1;
for(start=j,end=(start+w_len);start<end;start++,end--)
{
temp=str[start];
str[start]=str[end];
str[end]=temp;
}
j=i+1;
}
printf("After reversing the words string is %s \n",str);
}
Is This Answer Correct ? | 4 Yes | 1 No |
Answer / vinod kumar
#include"stdio.h"
#include"stdlib.h"
int reverse(char *string, char delimiter)
{
char *src, *dest;
char *temp = string;
while( *temp )
{
if (*temp == delimiter)
{
temp++;
continue;
}
src=dest=temp;
while ( (*(dest+1) != delimiter) &&
( *(dest+1) != '\0' )) dest++;
//( *(dest+1) != '\n' ) &&
temp=dest+1;
while( dest > src )
{
char tmp = *dest;
//*dest -- = *src;
*dest = *dest-- *src;
//*src++=tmp;
*src = *src++ tmp;
}
}
return 0;
}
int main()
{
char name[] = "vinod kumar dhochak";
printf("%s\n",name);
reverse(name,' '); /* space as delimiter,Reverse Words
*/
printf("%s\n",name);
reverse(name,'\n'); /* Reverse Complete Sentence */
printf("%s\n",name);
}
Is This Answer Correct ? | 5 Yes | 6 No |
Answer / ashish
this works in O(n) as first reverse all the sentence....then
reverse each word....
#include<iostream.h>
#include<conio.h>
#include<string.h>
char a[50];
void wrd_reverse()
{
void str_rev(int start_index,int len);
int t=strlen(a);
str_rev(0,t);
int i=0;int st_indx=0;int count=0;
int size;
while(a[i]!='\0')
{
if(a[i]!=' ')
count++;
else
{
size=count;
str_rev(st_indx,size);
count=0;
st_indx=i+1;
}
i++;
}
size=count;
str_rev(st_indx,size);
}
void str_rev(int start_index,int len)
{
int sft=start_index;
int lenm=len/2;
len--;
for(int i=0;i<lenm;i++)
{
a[sft+i]=a[sft+i]+a[(sft+len)-i];
a[(sft+len)-i]=a[sft+i]-a[(sft+len)-i];
a[sft+i]=a[sft+i]-a[(sft+len)-i];
}
}
int main(){
cin.getline(a,40);
void wrd_reverse();
wrd_reverse();
cout.write(a,40);
return 0;
}
Is This Answer Correct ? | 1 Yes | 2 No |
Answer / sujan
previously i hav read the question mistake so i hav jst given to reverese the whole sntence not a particular word so this one program will give u final ans....test this program...and if u hav any question please ask my through my emailid :sujan_faith@hotmail.com
#include<iostream>
using namespace std;
int main()
{
char str[]="sujan is name my haha";
string ans;
int n=0,j=0,t=0,a=0;
while (str[n]!='\0')
n++;
char temp[n];
char tem[n];
// cout<<n<<endl;
for(int i=n-1;i>=0;i--)
{
temp[j]=str[i];
j++;
if((str[i]==' ')||i==0)
{
// cout<<"i:"<<i<<endl;
// cout<<"j:"<<j<<endl;
for(int k=j-1;k>=t;k--)
{
tem[a]=temp[k];
a++;
}
t=a;
}
}
tem[a]='\0';
// temp[j]='\0';
// cout<<temp<<endl;
cout<<tem<<endl;
system("pause");
}
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / rahul shandilya
#include<iostream>
#include<vector>
using namespace std;
int main()
{
char str[]="rahul shandilya is going";
string ans;
int n=0;
while(str[n]!='\0')
n++;
//cout<<n;
bool flag=true;
for(int i=n-1;i>=0;i--)
{
if(str[i]==' ')
{
int m=i+1;
// cout<<m<<" ";
string temp;
while(str[m]!=' ' && m<n)
{
temp+=str[m];
m++;
}
//cout<<m<<" ";
if(flag)
{
ans+=temp;
flag=false;
continue;
}
if(flag==false)
{
ans+=' ';
ans+=temp;
}
}
}
cout<<ans;
system("pause");
return 0;
}
//tell me if there is a batter way to do it,i dont think my
//solution is efficent
Is This Answer Correct ? | 3 Yes | 5 No |
Answer / sujan
#include<iostream>
using namespace std;
int main()
{
char str[]="najus si eman ym";
string ans;
int n=0,j=0;
while (str[n]!='\0')
n++;
char temp[n];
// cout<<n;
for(int i=n-1;i>=0;i--)
{
temp[j]=str[i];
j++;
}
temp[j]='\0';
cout<<temp<<endl;
system("pause");
}
Is This Answer Correct ? | 0 Yes | 2 No |
Answer / raghuram.a
/*complexity-2n..check d prog. */
#include<iostream.h>
#include<string.h>
#include<stdio.h>
void rev_word(char str[20],int m,int n)
{
int i,l,k;
k=n-m+1;
if(k%2==0)
l=(k/2-1);
else
l=k/2;
k=n;
for(i=m;i<=m+l;i++)
{ count++;
char t=str[i];
str[i]=str[k];
str[k]=t;
k--;
}
}
int main()
{
char str[100];
int i,j=0;
cout<<"\n\nenter string:";
gets(str);
rev_word(str,0,strlen(str)-1);
for(i=0;i<=strlen(str);i++)
{ count++;
if(str[i]==' '||str[i]=='\0')
{
rev_word(str,j,i-1);
j=i;
while(str[j]==' ')
j++;
i=j;
}
}
cout<<"\n\nsentence with order of words reversed is:";
cout<<str;
return 0;
}
Is This Answer Correct ? | 0 Yes | 6 No |
main() { int c = 5; printf("%d", main||c); } a. 1 b. 5 c. 0 d. none of the above
program to Reverse a linked list
12 Answers Aricent, Microsoft, Ness Technologies,
int swap(int *a,int *b) { *a=*a+*b;*b=*a-*b;*a=*a-*b; } main() { int x=10,y=20; swap(&x,&y); printf("x= %d y = %d\n",x,y); }
main(int argc, char **argv) { printf("enter the character"); getchar(); sum(argv[1],argv[2]); } sum(num1,num2) int num1,num2; { return num1+num2; }
main() { int i=5; printf("%d",++i++); }
What is the output for the following program main() { int arr2D[3][3]; printf("%d\n", ((arr2D==* arr2D)&&(* arr2D == arr2D[0])) ); }
main() { int i; clrscr(); printf("%d", &i)+1; scanf("%d", i)-1; } a. Runtime error. b. Runtime error. Access violation. c. Compile error. Illegal syntax d. None of the above
Write a C program that defines a 2-dimentional integer array called A [50][50]. Then the elements of this array should randomly be initialized either to 1 or 0. The program should then print out all the elements in the diagonal (i.e. a[0][0], a[1][1],a[2][2], a[3][3], ……..a[49][49]). Finally, print out how many zeros and ones in the diagonal.
struct Foo { char *pName; char *pAddress; }; main() { struct Foo *obj = malloc(sizeof(struct Foo)); clrscr(); obj->pName = malloc(100); obj->pAddress = malloc(100); strcpy(obj->pName,"Your Name"); strcpy(obj->pAddress, "Your Address"); free(obj); printf("%s", obj->pName); printf("%s", obj->pAddress); } a. Your Name, Your Address b. Your Address, Your Address c. Your Name Your Name d. None of the above
What is wrong with the following code? int *foo() { int *s = malloc(sizeof(int)100); assert(s != NULL); return s; }
Write a c program to search an element in an array using recursion
main() { char p[ ]="%d\n"; p[1] = 'c'; printf(p,65); }