-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_string.c
More file actions
52 lines (38 loc) · 1.11 KB
/
reverse_string.c
File metadata and controls
52 lines (38 loc) · 1.11 KB
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
52
/*
Task: Create a program to reverse the strings. Given the following
function to use:-
void reverse_string(char *string)
Hint: Use two pointers (left and right) to loop through the string and reverse the characters
1. create a array containing the string to be reversed
2. input that array into the reverse_string function
3. create two pointers, left and right
4. initialise the right one on the right side of the string (end of string)
5. keep reversing the strings until left > right (they have met in the middle)
*/
#include <stdio.h>
#include <string.h>
void reverse_string(char *string){
char *left = string;
char *right = string;
while(*right != '\0'){
right++;
}
right--;
while(left < right){
char temp = *left;
*left = *right;
*right = temp;
left++;
right--;
}
}
int main(){
char str[100];
printf("Enter a string you would like to reverse: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0';
printf("Original: %s\n", str);
reverse_string(str);
printf("Reversed: %s\n", str);
return 0;
}