-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseString2.c
More file actions
55 lines (50 loc) · 1.19 KB
/
reverseString2.c
File metadata and controls
55 lines (50 loc) · 1.19 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
53
54
55
//
// Created by in4568 on 2026/4/18.
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char a[100];
char b[100] = {'\0'};
int k;
scanf("%s %d", a, &k);
int index = 0; //表示字符串起始位,每次自增2k
int len = strlen(a);
while (1) {
if (len - index <= k)
{
for (int i = index; i < len; i++)
{
b[i] = a[len - i - 1 + index];//剩下的字符数不到k,全部翻转
}
break;
}
else if (k < len - index && len - index <= 2*k)
{
for (int i = index; i < index + k; i++)
{
b[i] = a[index + k - 1- (i - index)];
}
for (int i = index + k; i < len; i++)
{
b[i] = a[i];
}
break;
}
else {
for (int i = index; i < index + k; i++)
{
b[i] = a[index + k - 1- (i - index)];
}
for (int i = index + k; i < index + 2 * k; i++)
{
b[i] = a[i];
}
index += 2 * k;
}
}
printf("%s",b);
return 0;
}