-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverse_the_sentence.c
More file actions
33 lines (33 loc) · 912 Bytes
/
reverse_the_sentence.c
File metadata and controls
33 lines (33 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*PROGRAM TO REVERSE THE SENTENCE IN A STRING*/
#include<stdio.h>
#include<string.h>
#define SIZE 30
void reverse_sentence(char *arr,int size);
void main(){
char arr[SIZE];
printf("Enter a Sentence: ");
scanf("%[^\n]",arr);
reverse_sentence(arr,SIZE);
printf("Sentence Reversed: %s\n",arr);
}
void reverse_sentence(char *arr,int size){
char temp_arr[size];
temp_arr[0]='\0'; //i.e. to make sure the garbage value in not concatenated in the string
strrev(arr);
char *token=strtok(arr," ");
while(token!=NULL){ //i.e. spliting the reversed string into sub-strings
strrev(token);
strcat(temp_arr," "); //i.e. adding spaces
strcat(temp_arr,token);
token=strtok(NULL," ");
}
int i; char temp;
for(i=0;1;i++){ //i.e. removing initial space
temp=temp_arr[i+1];
temp_arr[i]=temp_arr[i+1];
temp_arr[i+1]=temp;
if (temp=='\0')
break;
}
strcpy(arr,temp_arr);
}