Add hw1
This commit is contained in:
28
hw1/sample_string_reverse.c
Normal file
28
hw1/sample_string_reverse.c
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
#include <stdio.h>
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
char buf[50];
|
||||
|
||||
printf("Input string to reverse: ");
|
||||
fgets(buf, 50, stdin);
|
||||
|
||||
int max = 0;
|
||||
char *c = buf;
|
||||
while (*c++)
|
||||
max++;
|
||||
max--;
|
||||
buf[max] = '\0';
|
||||
max--;
|
||||
|
||||
for (int i = 0; i < max - i; i++) {
|
||||
int top = max - i;
|
||||
char temp = buf[top];
|
||||
buf[top] = buf[i];
|
||||
buf[i] = temp;
|
||||
}
|
||||
|
||||
printf("Reversed: %s\n", buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user