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 / tarak
#include<stdio.h>
int main()
{
char *p="i am working in TechM";
char *s,*temp;
char a[20];
int i=0;
s=p;
while(*p != '\0')
p++;
while(s != p){
while(*(--p)!=' ');
temp=p;
p++;
while(*p != '\0' && *p != ' ')
{
a[i++]=*p;
p++;
}
a[i++]=' ';
p=temp;
p--;
}
while(*s != ' ')
a[i++]=*s++;
a[i] = '\0';
printf("%s \n",a);
}
~
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / anubhav meena
#include <stdafx.h>
#include <string.h>
char* ReverseString(char *a){
int length = strlen(a);
printf("length is:%d\n",length);
int i=0;
int j=length-1;
while(i<j){
*(a+i)=*(a+i)^*(a+j);
*(a+j)=*(a+i)^*(a+j);
*(a+i)=*(a+i)^*(a+j);
i++;
j--;
}
return a;
}
int main()
{
char s[] = "katrina kaif is gorgeous gal!";
char *a=s;
char *b=s;
char *e= a+strlen(a);
printf("String is:%s\n",a);
ReverseString(a);
printf("NewString is:%s\n",a);
int i=0;
while(*(a+i)!='\0'){
while(*(a+i)!=' ' && *(a+i)!='\0') i++;
*(a+i)='\0';
ReverseString(a);
if((a+i)!=e){
*(a+i)=' ';
a = a+i+1;
}
else{
break;
}
i=0;
}
printf("FinalString is:%s\n",b);
getchar();
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / abdur rab
#include <stdio.h>
void reverse_string ( char* cp_string, int start, int end )
{
if ( cp_string && ( start < end ) ) {
*( cp_string + start ) ^= *( cp_string +
end )
^= *( cp_string + start ) ^= *(
cp_string + end );
reverse_string ( cp_string, ++start, --
end );
}
}
int main ( int argc, char* argv [] )
{
char ca_array [12]={"Hello World"};
char* cp_ptr = NULL;
char* cp_ptr_temp = NULL;
printf ( "\n Before Reverse :%s", ca_array );
reverse_string ( ca_array, 0, strlen ( ca_array ) -
1 );
cp_ptr_temp = cp_ptr = ca_array;
while ( NULL != ( cp_ptr_temp = (char*) strchr (
cp_ptr_temp, ' ' ) ) ) {
reverse_string ( cp_ptr, 0, ( cp_ptr_temp -
cp_ptr ) - 1 );
cp_ptr = ++cp_ptr_temp;
}
// change the final word
reverse_string ( cp_ptr, 0,
( ( strlen ( ca_array ) - 1 ) - (
cp_ptr - ca_array ) ) );
printf ( "\n After Reverse by Words :%s",
ca_array );
return ( 0 );
}
Is This Answer Correct ? | 1 Yes | 0 No |
Give the output for the following program. #define STYLE1 char main() { typedef char STYLE2; STYLE1 x; STYLE2 y; clrscr(); x=255; y=255; printf("%d %d\n",x,y); }
what is array?
Program to find the sum of digits of a given number until the sum becomes a single digit. (e.g. 12345=>1+2+3+4+5=15=>1+5=6)
How can I make it pause before closing the program output window?
How can you convert integers to binary or hexadecimal?
what is an array
write a programming in c to find the sum of all elements in an array through function.
How to declare a variable?
Main must be written as a.the first function in the program b.Second function in the program c.Last function in the program d.any where in the program
19 Answers CTS, HCL, TCS,
Can you assign a different address to an array tag?
Average of a couple 10 years ago was 25. The average remains same after having a child and twins after 3 years. What is the present age of the first child
List some of the static data structures in C?