pgm to reverse string using arrays i.e god is love becomes
love is god)
(assumption:only space is used for seperation of words)

no addtional memory used.i.e no temporary arrays can used.

Answers were Sorted based on User's Feedback



pgm to reverse string using arrays i.e god is love becomes love is god) (assumption:only space i..

Answer / santhi perumal

#include<stdio.h>
#include<conio.h>

int main()
{

int i,j,length;
char a[] = "god is love";

length = strlen(a);

printf("The Given String is\n%s\n",a);
printf("The Resulted String is\n");

for(i=length-1;i>=0;i--)
{
if(a[i] == ' ')
{
for(j=i+1; a[j] != ' ' && a[j] != '\0';j++)
{
printf("%c",a[j]);
}
printf("%c",a[i]);
}
}
i++;
while(a[i] !=' ')
{
printf("%c",a[i]);
i++;
}
printf("\n");

}

Is This Answer Correct ?    14 Yes 2 No

pgm to reverse string using arrays i.e god is love becomes love is god) (assumption:only space i..

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

pgm to reverse string using arrays i.e god is love becomes love is god) (assumption:only space i..

Answer / ruchi

#include<stdio.h>
#include<conio.h>
int main()
{
int i=0,c;
char a[20];
printf("\nEnter the string");
while((a[i++]=getchar())!='\n');
printf("\nReverse of the string is ");
for(c=i;c>=0;c++)
{
printf("%c"a[c]);
}
getch();
}

Is This Answer Correct ?    1 Yes 5 No

pgm to reverse string using arrays i.e god is love becomes love is god) (assumption:only space i..

Answer / paaru

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],i;
printf("\n\nEnter the string:");
gets(a);
for(i=strlen(a)-1;i>=0;i++)
{
printf("%d"a[i]);
}
getch();
}

Is This Answer Correct ?    1 Yes 7 No

Post New Answer

More C Interview Questions

Write a program to print all permutations of a given string.

0 Answers   JPMorgan Chase,


main() { intj; while9j<=10) { printf("\n%d",j); j=j+1; } }

4 Answers  


How can I call system when parameters (filenames, etc.) Of the executed command arent known until run time?

0 Answers  


What are the different types of pointers?

4 Answers   HCL, TCS,


How to reverse a linked list

1 Answers   Aricent, Fidelity, IBM, TCS,






write a program structure to find average of given number

1 Answers  


What is the difference between break and continue?

0 Answers  


How can I implement sets or arrays of bits?

0 Answers  


What is the difference between variable declaration and variable definition in c?

0 Answers  


main() { float a=8.8; double b=8.8; if(a==b) printf("Equal"); else printf("not equal"); getch(); } what is the output? with reason

6 Answers   CSC,


a linearly ordered set of data elements that have the same structure and whose order is preserved in storage by using sequential allocation a) circular b) ordinary c) array d) linear list

0 Answers  


write a program of palindrome(madam=madam) using pointer?

5 Answers   L&T,


Categories