在C中向前移动一个字符三个空格



编写一个函数,使用strrot函数,修改n个字符串中给定数组s的每个字符串。功能原型为:

void rot_all(char *s[], int n);

这是我的代码,我只是不知道如何做最后一个函数。

char rot3(char c) // Function shifts a letter by three spaces (eg 'a' -> 'd')
{
if (c >= 'A' && c <= 'Z') return c += 3;
if (c >= 'a' && c <= 'z') return c += 3;
return c;
}

void strrot(char *str)
{
int d = strlen (str);
for (int i = 0; i < d; i++)
{
str[i] = rot3(str[i]);
}
}
void rot_all(char *s[], int n)
{

}
int main ()
{
char str[23];
scanf("%s",str);
strrot(str);
printf("%s",str);
return 0;
}

假设c是小写字母或大写字母。使用assert()对此进行记录。

#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char rot3(char c) {
assert(isalpha(c));
char offset = isupper(c) ? 'A' : 'a';
return offset + (c - offset + 3) % ('Z' - 'A' + 1);
}
void strrot(char *str) {
for (int i = 0; str[i]; i++) {
str[i] = rot3(str[i]);
}
}
void rot_all(char *s[], int n) {
for(int i = 0; i < n; i++) {
strrot(s[i]);
}
}
int main () {
char *strs[] = {
strdup("Hello"),
strdup("World")
};
rot_all(strs, sizeof(strs) / sizeof(*strs));
for(int i = 0; i < sizeof(strs) / sizeof(*strs); i++) {
printf("%sn", strs[i]);
free(strs[i]);
}
return 0;
}

输出为:

Khoor
Zruog

最新更新