Category >
CPLUSPLUS
|| Published on :
Sunday, May 17, 2015 || Views:
7419
||
How to reverse a sentence with a program C Program to reverse a sentence with a program reverse a sentence with a program
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char *s = "this is ball", ch;
int len = strlen(s), start, end = -1, t = 0, length = 0, i;
clrscr();
printf("Original sentence=%s\n", s);
*(s + len + 1) = '';
*(s + len) = ' ';
while (*(s + length) != NULL) {
if (*(s + length) == ' ') {
start = end + 1;
end = length;
//printf("%d %d\n",start,end);
t = 0;
for (i = start; i < start + (end - start) / 2 + 1; i++) {
ch = *(s + i);
*(s + i) = *(s + end - t);
*(s + end - t) = ch;
t++;
}
}
length++;
}
strrev(s);
printf("After processing=%s", s);
getch();
}
This is a very simple program having the following logic. If the main string is say: ‘this is ball’. then after reversing words at their respective places, the string will read as ‘siht si llab’, reversing this string we get – ‘this is ball’
so In this tutorial we learn How to reverse a sentence with a program