-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse_sentence.c
51 lines (43 loc) · 1.02 KB
/
reverse_sentence.c
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
char* ReverseSentence(char* src);
void Mirror(char* str_start, char* str_end);
int main()
{
char string1[] = "Have a nice day";
ReverseSentence(string1);
printf("the new string is \n%s\n", string1);
return (0);
}
char* ReverseSentence(char* src)
{
char* str_ptr1 = src;
char* str_ptr2 = src;
while (*str_ptr2 != '\0')
{
if (*str_ptr2 == ' ')
{
Mirror(str_ptr1, str_ptr2-1);
str_ptr1 = str_ptr2 +1;
}
++str_ptr2;
}
Mirror(str_ptr1,str_ptr2 - 1);
Mirror(src,str_ptr2 - 1);
return(src);
}
void Mirror(char* str_start, char* str_end)
{
char temp_char = 0;
char *str_end_ptr = str_end;
while (str_start < str_end_ptr)
{
temp_char = *str_start;
*str_start = *str_end_ptr;
*str_end_ptr = temp_char;
++str_start;
--str_end_ptr;
}
}