|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
当n=1
时,今天写的每一句话中所用的字母都会向后+1,比如:i love you就成了j mpwf zpv,当然了当是z的时候,+1就等于a。 输入为多组,每组为两行。
第一行为n
,−50<n<50
第二行为一句话,只有小写字母和空格。长度小于10000
#include <stdio.h>
#include <string.h>
int main(){
int n, m, i;
char a[10001];
while(scanf("%d", &n)==1)
{
int s=strlen(a);
fgets(a, 10001, stdin);
for(i=0;i<s;i++)
{
if(a[i]!=' ')
{
m=a[i]-'a';
n=n%26;
m=(m-n)%26;
a[i]=m+'a';
}
printf("%c\n",a[i]);
}
}
return 0;
}
好的,下面是使用C语言实现字符移位加密的示例代码:
- #include <stdio.h>
- #include <ctype.h>
- void shift_characters(char *text, int n) {
- for (int i = 0; text[i] != '\0'; i++) {
- if (isalpha(text[i])) {
- char base = islower(text[i]) ? 'a' : 'A';
- text[i] = ((text[i] - base + n) % 26 + base);
- }
- }
- }
- int main() {
- int test_cases;
- printf("请输入测试数据的组数:");
- scanf("%d", &test_cases);
- getchar(); // consume the newline character
-
- for (int i = 0; i < test_cases; i++) {
- int n;
- printf("请输入移动的位置数:");
- scanf("%d", &n);
- getchar(); // consume the newline character
-
- printf("请输入需加密的文本:");
- char text[100];
- fgets(text, sizeof(text), stdin);
-
- shift_characters(text, n);
- printf("加密后的文本:%s\n", text);
- }
-
- return 0;
- }
复制代码
这段代码使用了fgets函数来获取输入的文本,以避免使用scanf时可能出现的换行符问题。shift_characters函数对每个字符进行判断和移位加密,并直接修改原始的文本字符串。
通过循环输入测试数据的组数以及移动的位置数和需要加密的文本,然后调用shift_characters函数进行加密,并打印加密后的结果。
希望这段C语言代码可以帮助到你。如果还有其他问题,请随时提问。
以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
|
|