-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathT01-2-StringRev.c
32 lines (27 loc) · 882 Bytes
/
T01-2-StringRev.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
#include <stdio.h>
#include <string.h>
// function definition of the revstr()
void revstr(char *str1)
{
// declare variable
int i, len, temp;
len = strlen(str1); // use strlen() to get the length of str string
// use for loop to iterate the string
for (i = 0; i < len/2; i++)
{
// temp variable use to temporary hold the string
temp = str1[i];
str1[i] = str1[len - i - 1];
str1[len - i - 1] = temp;
}
}
int main()
{
char str[50]; // size of char string
printf ("Enter the string: ");
fgets(str, sizeof(str), stdin);; // use gets() function to take string
printf ("\nBefore reversing the string: %s \n", str);
// call revstr() function
revstr(str);
printf ("After reversing the string: %s", str);
}