gpt4 book ai didi

c - 需要创建一个函数来编码一个单词

转载 作者:行者123 更新时间:2023-11-30 20:14:08 26 4
gpt4 key购买 nike

编写一个函数,该函数获取一个 char 数组(用于编码的文本)作为参数。该函数需要对该文本进行编码。编码是这样完成的:字母 a 与字母 z 交换,b 与 y 等交换(字母表的第一个字母与字母表的最后一个字母交换,第二个字母交换)字母表中的 b 与 y 交换。

示例:阿巴

结果:zyyz

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int funk(char *alphabet,int i, int n,char *word,int j,int x){
while(i<n){
if(word[i]=alphabet[j]){
word[i]=alphabet[x-i];
i++;
}
else
j++;
}

return word[i];
}

void main ()
{
int i=0,j=0,n,x;
char alphabet[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char word[10];

n=strlen(word);
x=strlen(alphabet);
printf("Enter your word :\n");

for(i=0;i<n;i++) {
scanf("%s",word[i]);
}

{
word=funk(n,azbuka,word,x,i,j);

for(i=0;i<n;i++){
printf("%s",word[i]));
}
}

return 0;
}

最佳答案

您的程序可以大大简化。 “编码”逻辑的逻辑应该只有几行代码。您的函数声明/原型(prototype)也非常复杂。不要忘记,C 字符串只是一个以 NULL 字符 ('\0') 结尾的字符数组。

代码列表

<小时/>
/*******************************************************************************
* Preprocessor directives
******************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <string.h>


/*******************************************************************************
* Constants
******************************************************************************/
#define MAX_BUF_SIZE

/*******************************************************************************
* Function Prototypes
******************************************************************************/
void encode(char* str, size_t length);


/*******************************************************************************
* Function Definitions
******************************************************************************/
void encode(char* str, size_t length) {
if (!str || (length <= 1)) {
return;
}
int i;
for (i=0; i<length; i++) {
if (isalpha(str[i])) {
if (isupper(str[i])) {
str[i] = 'Z' - str[i] + 'A';
} else {
str[i] = 'z' - str[i] + 'a';
}
}
}
}

int main(void) {
char buf1[] = "This is a test";
char buf2[] = "abcdefghijklmnopqrstuvwxyz";
char buf3[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char* inputStrings[] = {buf1, buf2, buf3};
char* buf;
int i;

for (i=0; i<3; i++) {
buf = inputStrings[i];
printf("Original String: %s\n", buf);
encode(buf, strlen(buf));
printf("Encoded String: %s\n", buf);
encode(buf, strlen(buf));
printf("Decoded String: %s\n", buf);
}

return 0;
}

示例运行

<小时/>
Original String: This is a test
Encoded String: Gsrh rh z gvhg
Decoded String: This is a test
Original String: abcdefghijklmnopqrstuvwxyz
Encoded String: zyxwvutsrqponmlkjihgfedcba
Decoded String: abcdefghijklmnopqrstuvwxyz
Original String: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encoded String: ZYXWVUTSRQPONMLKJIHGFEDCBA
Decoded String: ABCDEFGHIJKLMNOPQRSTUVWXYZ

关于c - 需要创建一个函数来编码一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27694996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com