繁体   English   中英

C 编程 - 如何用另一个字符更改字符串中每个出现的字符

[英]C Programming - How to change every occurrence of character in a string with another character

任务是让用户插入一个字符串,程序将 output 一条秘密消息,用另一个字符更改该字符串的每个字符的出现。 将插入的新字符列表由排列“qjczieaungsdfxmphybklortvw”给出,它对应于字母表的每个字母。 例如,字符串“abcxyz”将返回“qjctvw”。 程序将忽略符号和大写字母,因此“Abc”将变为“Ajc”。

我试图通过将字符串的每个 position 与字母表中的每个字母进行比较来实现这一点。 如果匹配,则字符串的 position 将替换为与传统字母表的 position 相同的秘密排列的 position(因为它们对应)。 该代码在技术上有效,但我没有得到正确的值。 例如,对于每个“a”,我应该得到一个“q”,但返回的是一个“h”。 如果有人可以修复我的代码,将不胜感激。 下面的代码:请复制并粘贴到您喜欢的代码编辑器中,看看我返回错误值的意思。

#include <string.h>
#define MAX_STR_LEN 256
int main(void)
{
  char perm[] = "qjczieaungsdfxmphybklortvw";
  char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
  int i, j;
  char msg[MAX_STR_LEN+1];

  /* read the message from the terminal using fgets. The variable msg will contain the message. */
  fgets(msg, MAX_STR_LEN, stdin);

/*compares each value of the string to the alphabet*/
  for (i = 0; i < (strlen(msg) - 1); i++) {
    for (j = 0; j < (strlen(alphabet) - 1); j++) {
       if (msg[i] == alphabet[j]) {msg[i] = perm[j];}
     }
  }
  printf("%s", msg);
}

对于调用 fgets 后的初学者

fgets(msg, MAX_STR_LEN, stdin);

您需要删除 function 可以附加到输入字符串的换行符。

msg[ strcspn( msg, "\n" ) ] = '\0';

for 循环也不正确。 您可以使用 function strchr 例如

/*compares each value of the string to the alphabet*/
for ( size_t i = 0; msg[i] != '\0';  i++ ) 
{
    const char *p = strchr( alphabet, msg[i] );

    if ( p != NULL )
    {
        msg[i] = perm[p - alphabet];
    }
}
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#define MAX_STR_LEN 256

int main(void)
{
  char perm[] = "qjczieaungsdfxmphybklortvw";
  char msg[MAX_STR_LEN+1];

  /* read the message from the terminal using fgets. The variable msg will contain the message. */
  fgets(msg, MAX_STR_LEN, stdin);

  for (unsigned short i = 0; i < strlen(msg) - 1; ++i)
    if (islower(msg[i]))
        msg[i] = perm[i];
    printf("%s", msg);

    return 0;
}

尝试这个

一旦您找到问题的根源并观察到:

  1. 您的字母替换字符串只是一个简单的数组,其字母值映射到从 0 到 25 的索引。
  2. 字符本身有一些由 ASCII 标准指定的 integer 值,它们恰好是连续('b'=='a'+1)

有了这些知识,我们可以发明一个 function,它接受一个字符并将其转换为perm字符串中正确的 position。 我们可以通过非常简单的方式做到这一点,只需从我们拥有的任何字符中减去'a' 'a'-'a'将等于 0,依此类推。

这可以像这样实现为 function :(或者你可以内联它,因为它很简单)

int get_char_index(char c){
    return c - 'a';
}

现在,可以访问这个 function,我们需要做的就是逐个字符地迭代字符串(for 循环,while 循环,没关系),从字符串中读取字符,将其提供给 function,然后然后,一旦您获得perm的相应索引,您只需将该索引下的 char 的值分配回字符串。

int main() {
    const char perm[] = "qjczieaungsdfxmphybklortvw";
    
    char some_string_to_convert[] = "aabbccddeeffgghhiijjkkllmmnnoopprrssttqqvvwhatever";
    for(int i=0; i<strlen(some_string_to_convert); i++){
        char c = some_string_to_convert[i];
        int perm_index = get_char_index(c);
        some_string_to_convert[i] = perm[perm_index];
    }

    printf("%s", some_string_to_convert);
}

以下代码输出: qqjjcczziieeaauunnggssddffxxmmppyybbkkhhooruqkioiy

希望我的回答能帮助你解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM