GeekInterview.com
Series: Subject: Topic:
Question: 446 of 587

How to reverse a sentence with C program.

Asked by: Interview Candidate | Asked on: Sep 13th, 2005
Showing Answers 1 - 23 of 23 Answers
Sandeep

Answered On : Oct 25th, 2005

Take sentence in an array and get array size and then print the array in ascending order.

Yes  1 User has rated as useful.
  
Login to rate this answer.
samrat

Answered On : Feb 8th, 2006

A more undo answer can be like this

rever(char a )

{

  
Login to rate this answer.
Casanova

Answered On : Mar 18th, 2006

View all answers by Casanova

I think what the question requires is that only the words in the sentence be reversed. For this follow this pseudo code:for each word in sentence: reverse (word)print reverse (sentence)for e.g. given 'an indian on road' after reversing each word we get 'na naidni no daro' on reversing this we get ' road on indian an'

  
Login to rate this answer.

Take the string in an array and continue loop till the half length of array and swap the characters which are at equals from both sides of array.

  
Login to rate this answer.
Harshdev Joshi

Answered On : Mar 16th, 2007

Program...

Code
  1. #include
  2. #include
  3. #include
  4. void main()
  5. {
  6.     char *s = "Life is beautiful", ch;
  7.     int len = strlen(s), start, end = -1, t = 0, length = 0, i;
  8.     clrscr();
  9.     printf("Original sentence=%sn", s);
  10.     *(s + len + 1) = '';
  11.     *(s + len) = ' ';
  12.     while (*(s + length) != NULL) {
  13.         if (*(s + length) == ' ') {
  14.             start = end + 1;
  15.             end = length;
  16. //printf("%d %dn",start,end);
  17.             t = 0;
  18.             for (i = start; i < start + (end - start) / 2 + 1; i++) {
  19.                 ch = *(s + i);
  20.                 *(s + i) = *(s + end - t);
  21.                 *(s + end - t) = ch;
  22.                 t++;
  23.             }
  24.         }
  25.         length++;
  26.     }
  27.     strrev(s);
  28.     printf("After processing=%s", s);
  29.     getch();
  30. }
  31.  

This is a very simple program having following logic. Main string - Life is beautiful
After reversing words at their respective places - efiL si lufituaeb
Reversing it we get - beautiful is Life

Yes  1 User has rated as useful.
  
Login to rate this answer.
srinivasarao

Answered On : Oct 12th, 2007

A small code for this....

Code
  1. #include
  2. #include
  3. #include
  4. void main()
  5. {
  6.     int i, l;
  7.     char s[] = "this is string reverse";
  8.     clrscr();
  9.     l = strlen(s);
  10.     for (i = l; i >= 0; i--)
  11.         printf("%c", s[i]);
  12.     getch();
  13. }
  14.  

  
Login to rate this answer.
kaushalgoa

Answered On : Dec 31st, 2007

View all answers by kaushalgoa

Code
  1. #include "stdio.h"
  2. #include "string.h"
  3. #include "stdlib.h"
  4. void main()
  5. {
  6.     char *p, ch;
  7.     int x = 0, y = 0, z = 0;
  8.     p = malloc(10);
  9.     printf("Enter a Wordn");
  10.     scanf("%s", p);
  11.     printf("%sn", p);
  12.     z = strlen(p);
  13.     x = (z) / 2;
  14.     for (y = 0; y < x; y++) {
  15.         ch = *(p + y);
  16.         *(p + y) = *(p - y + z - 1);
  17.         *(p - y + z - 1) = ch;
  18.     }
  19.     printf("%sn", p);
  20.     return;
  21. }

// Note (backslash not displayed on this website.)

  
Login to rate this answer.

Hi Srinivas rao i think there is a small change required in ur program
coorect is
l=strlen(s)-1;

  
Login to rate this answer.
fkhanoom

Answered On : Mar 19th, 2008

View all answers by fkhanoom

Code
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #define MAX 100
  4. void revStr ( char * );
  5. int main(){
  6. char *s = malloc( sizeof(char) * MAX );
  7. printf( "Enter a string (MAX characters: 100): " );
  8. //scanf( "%s", s );
  9. gets( s );
  10. assert( sizeof(s) <= sizeof(char)*MAX );
  11. revStr(s);
  12. printf( "%s", s );
  13. free( s );
  14. return 0;
  15. }
  16. void revStr( char *s ){
  17. char *headPtr = s;
  18. char *tailPtr = s;
  19. while ( *tailPtr != '' )
  20. ++tailPtr;
  21. for ( --tailPtr; headPtr < tailPtr; ++headPtr, --tailPtr ){
  22. char temp = *headPtr;
  23. *headPtr = *tailPtr;
  24. *tailPtr = temp;
  25. }
  26. }

  
Login to rate this answer.

Code
  1.  
  2. int main()
  3. {
  4.     char sentence[50];
  5.     cin >> sentence;
  6.     for (i = 0; i != 'n'; i++)
  7.         printf("%c", s[i]);
  8.     return 0;
  9. }
  10.  

  
Login to rate this answer.
ananth3335

Answered On : Mar 31st, 2008

View all answers by ananth3335

I think by using this code can do the reverse opeartion

Code
  1. main()
  2. {
  3.     char c;
  4.     if ((c = getchar()) != 'n')
  5.         main();
  6.     printf("%c", c);
  7.     getch();
  8. }

when u enter the 'enter key' it will terminate.

  
Login to rate this answer.

// A valid sentence is a group of words ended with a fullstop i.e.  with a dot( . ).
int main()
{

char sentence[50];
cin>>sentence;   //Every sentence must ended with a dot.

//print the sentence
for(int i=0; i!=' . ' ;i++)
cou<<sentence[i] ;


//reverse the sentence
int length= strlen(sentence);

// suppose the length of the sentence is 10 then fullstop is in the 10th position. For reversing the sentence we have to print from 10th-1 position to the 0th position.

for(int j=length-1; j>=0; j-- )
{
cout<<sentence[j];
}

return 0;
}

  
Login to rate this answer.
Nihar Shukla

Answered On : Apr 4th, 2008

View all answers by Nihar Shukla

Hey srinivasarao,

You are not actually reversing the string !!
You are just printing the string in reverse order.

  
Login to rate this answer.

Code
  1.  
  2. #include
  3. #include
  4. #include
  5. #define size 100
  6. reverse(char *a, char *b)
  7. {
  8.     char temp = *a;
  9.     *a = *b;
  10.     *b = temp;
  11. }
  12. main()
  13. {
  14.     char str[size];
  15.     int i, j, len, haf, k = 1;
  16.     puts("nEnter a string of msx 100 charactersn");
  17.     gets(str);
  18.     len = strlen(str);
  19.     haf = len / 2;
  20.     printf("the given string B4 reverse isn");
  21.     for (i = 0; i < len; i++)
  22.         printf("%c", str[i]);
  23.     for (i = 0, j = haf - 1; i < haf && j > haf; i++, j--)
  24.         revers(&str[i], &str[j]);
  25.     printf("nReverse of given string isn");
  26.     for (i = 0; i < len; i++)
  27.         printf("%c", str[i])
  28.         }
  29.  

Let us assume that the entered string is : parents are gods
the output is as follows

The given string B4 reverse is :: Parents are gods
After reverse of string is :: sdog era stnerap

  
Login to rate this answer.
kernel32

Answered On : May 24th, 2008

View all answers by kernel32

Code
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. void reverseString(char *ch)
  6. {
  7.     int index;
  8.     int len = strlen(ch);
  9.     int mid = len / 2;
  10.     char *temp;
  11.  
  12.     for (index = 0; index < mid; index++) {
  13.         temp = ch[index];
  14.         ch[index] = ch[(len - 1) - index];
  15.         ch[(len - 1) - index] = temp;
  16.     }
  17. }
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.     char *c = (char *) malloc(100 * sizeof(char));
  22.     gets(c);
  23.     printf("Before reversal => %sn", c);
  24.     reverseString(c);
  25.     printf("After first reversal => %sn", c);
  26.     reverseString(c);
  27.     printf("After second reversal => %sn", c);
  28.     if (c) {
  29.         free(c);
  30.     }
  31.     return 0;
  32. }
  33.  

  
Login to rate this answer.
thtsarjun

Answered On : Sep 27th, 2009

View all answers by thtsarjun

I think the question is revere the order of words in the sentence. Because reversing a sentence doesn't make a sense when you can simply copy one string to other in reverse order and do. Here is what I did and it does reverse the order of spaces used in the sentence:

Code
  1.  
  2.  
  3. #include <stdio.h>
  4. char *reverse_word(char *s)
  5. {
  6.     char *str = s;
  7.     int len = string_len(s);
  8.     int i = 0;
  9.     int count = 0;
  10.     reverse_substring(str, 0, pos_lastChar(str));
  11.     while (i <= len) {
  12.         if (str[i] == ' ' || i == len) {
  13.             reverse_substring(str, i - count, i - 1);
  14.             count = 0;
  15.         }
  16.         if (str[i] != ' ') {
  17.             count++;
  18.         }
  19.         i++;
  20.     }
  21.     return str;
  22. }
  23.  
  24. int pos_lastChar(char *s)
  25. {
  26.     int i;
  27.     for (i = string_len(s); i > 0; i--) {
  28.         if (s[i] != ' ' && s[i] != '') {
  29.             return i;
  30.         }
  31.     }
  32. }
  33.  
  34. reverse_substring(char *s, int start, int end)
  35. {
  36.     char temp;
  37.     int i, j;
  38.     for (i = start, j = end; i < j; i++, j--) {
  39.         temp = s[i];
  40.         s[i] = s[j];
  41.         s[j] = temp;
  42.     }
  43. }
  44.  
  45. int string_len(char *str)
  46. {
  47.     int x = 0;
  48.     while (*str++) {
  49.         x++;
  50.     }
  51.     return (x);
  52. }
  53.  
  54. int main(int argc, char **argv)
  55. {
  56.     char str[] = "this is very beautiful";
  57.     printf("nGiven String : %sn", str);
  58.     printf("Length of string: %dn", string_len(str));
  59.     printf("Reversed string: %sn", reverse_word(str));
  60.     return 0;
  61. }
  62.  
  63.  

  
Login to rate this answer.
opal_sybil

Answered On : Aug 27th, 2010

View all answers by opal_sybil

Code
  1.  
  2. #include <stdlib.h>
  3. int main(void)
  4. {
  5.     const char sentence[] = "When marimba rhythm starts to play";
  6.     char *array[50];
  7.     int loop;
  8.     array[0] = (char *) strtok(sentence, " ");
  9.     if (array[0] == NULL) {
  10.         printf("No test to search.n");
  11.         exit(0);
  12.     }
  13.     for (loop = 1; loop < 50; loop++) {
  14.         array[loop] = (char *) strtok(NULL, " ");
  15.         if (array[loop] == NULL)
  16.             break;
  17.     }
  18.     printf("Original sequence is: ");
  19.     for (loop = 0; loop < 50; loop++) {
  20.         if (array[loop] == NULL)
  21.             break;
  22.         printf("%s ", array[loop]);
  23.     }
  24.     printf("nSequence backwards is: ");
  25.     for (--loop; loop >= 0; loop--) {
  26.         if (array[loop] != NULL) {
  27.             printf("%s ", array[loop]);
  28.         } else
  29.             break;
  30.     }
  31.     printf("n");
  32.     return 0;
  33. }
  34.  

  
Login to rate this answer.
vicky_svk

Answered On : Sep 27th, 2010

View all answers by vicky_svk

Code
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<string.h>
  5.  
  6. void main()
  7. {
  8.         char s[50],str[50][50];
  9.         int len,i=0,j=0,k=0,l=0,p=0,m;
  10.         clrscr();
  11.         printf("Enter the sentencen");
  12.         gets(s);
  13.         len=strlen(s);
  14.         while(i<len)
  15.         {
  16.                 j=0;
  17.  
  18.                 while(1)
  19.                 {
  20.                         if((s[i] == ' ') || (s[i] == '')){
  21.                         i++;
  22.                         break;
  23.                         }
  24.                         str[k][j++]=s[i];i++;
  25.                 }
  26.                 str[k][j]='';
  27.                 k++;
  28.         }
  29.         /*printf("%c",str[0][0]); */
  30.         l=--k;
  31.         while(l>=0)
  32.         { p=0;
  33.         while(str[l][p]!='')
  34.                 {
  35.                         printf("%c",str[l][p]);
  36.                 p++;
  37.                 }
  38.                 printf(" ");
  39.                 l--;
  40.         }
  41.         getch();
  42. }
  43.  

  
Login to rate this answer.
rocking.piyush

Answered On : Feb 23rd, 2011

View all answers by rocking.piyush

Code
  1.  
  2. main()
  3. {
  4.     char s[100], r[100];
  5.     printf("Enter the sentence:n");
  6.     gets(s);
  7.     int len = strlen(s);
  8.     printf("%dn", len);
  9.     char *p = s + len - 1;
  10.     char *q = r;
  11.     int j, i = 0, c = 0, flag = 0;
  12.     while (c <= len) {
  13.         while (*p == ' ') {
  14.             p--;
  15.             for (j = 1; j <= (i - flag); j++)
  16.                 printf("%c", *(q - j));
  17.             c++;
  18.             flag = i;
  19.             printf(" ");
  20.         }
  21.         *q = *p;
  22.         q++;
  23.         p--;
  24.         i++;
  25.         c++;
  26.     }
  27.     for (j = 1; j <= (i - flag); j++)
  28.         printf("%c", *(q - j));
  29.     printf("n");
  30. }
  31.  

  
Login to rate this answer.
rizwann

Answered On : Nov 29th, 2011

View all answers by rizwann

Here is the code to reverse the sentence that does not use any in built string function. if the input is:Life is good output will be good is Life ;)

Code
  1.  
  2. #include<stdio.h>
  3. void reverse(char *, char *);
  4. int main()
  5. {
  6.     /*Declaration and initialization */
  7.     char *begin = "Life is Good", *end, *start, *len, *space;
  8.     start = begin;
  9.     end = begin;
  10.     /*Find length of string */
  11.     printf("%s", start);
  12.     len = begin;
  13.     while (*len)
  14.         len++;
  15.     *len = ' ';
  16.     *(++len) = '';
  17.     len--;
  18.  
  19.     /*Reverse individual words */
  20.  
  21.     while (*end) {
  22.         if (*end == ' ') {
  23.             space = end;
  24.             reverse(begin, end - 1);
  25.             begin = space + 1;
  26.             end = space;
  27.         }
  28.         end++;
  29.     }
  30.     *(--end) = '';
  31.     /*Reverse the entire string */
  32.     char *s1 = "";
  33.     s1 = start;
  34.     printf("
  35. ");
  36.     reverse(start, len - 1);
  37.     printf("%s", s1);
  38.     return 0;
  39. }
  40.  
  41. void reverse(char *begin, char *end)
  42. {
  43.     char *temp = "";
  44.     while (end >= begin + 1) {
  45.         *temp = *begin;
  46.         *begin = *end;
  47.         *end = *temp;
  48.         end--;
  49.         begin++;
  50.     }
  51. }
  52.  

  
Login to rate this answer.
kala725

Answered On : Dec 11th, 2011

View all answers by kala725

Code
  1.  
  2. n = strlen(a);
  3. for (i = n - 1; i >= 0; i--)
  4. {
  5.     printf("%c", a[i]);
  6. }
  7.  

  
Login to rate this answer.
sachin

Answered On : Dec 13th, 2011

I think you should use reverse string programme. You can store it an array and then run a loop.

  
Login to rate this answer.
Harish

Answered On : Mar 21st, 2012

Code
  1. void main()
  2. {
  3.  
  4.         char *chStrptr = "Hi This is Harish";
  5.        
  6.  
  7.         While(*ChStrPtr != /0)
  8.         {
  9.                 LenOfStr++
  10.                 chStrPtr++;
  11.         }
  12.  
  13.  
  14.         do
  15.         {
  16.                 while(*chStrPtr !=  )
  17.                 {
  18.                         chStrPtr--;
  19.                         LenOfStr--;
  20.                        
  21.                 }
  22.  
  23.                 PrintWordsWithNullOrSpace(chStrPtr + 1);
  24.                                
  25.         }while(LenOfStr != 0);
  26.  
  27.  
  28. }
  29.  
  30. void PrintWordsWithNullOrSpace(const Char *StrPtr)
  31. {
  32.  
  33.         while((*strPtr != /0) || (*strPtr !=  ))
  34.         {
  35.                 printf("%c", *StrPtr);
  36.         }
  37. }
  38.  
  39.  

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.