如何在c中替换字符数组中的字母? #包括 #包括 #包括 int main() { 字符优先[15]; printf(“\n\t在此处写入您的代码”); scanf(“%s”,第一个); if()//字符中有“n” //用1更改n 其他的 //退出 返回0; } 用字符替换字符

如何在c中替换字符数组中的字母? #包括 #包括 #包括 int main() { 字符优先[15]; printf(“\n\t在此处写入您的代码”); scanf(“%s”,第一个); if()//字符中有“n” //用1更改n 其他的 //退出 返回0; } 用字符替换字符,c,C,此代码将用字符'1'替换出现的字符'n' #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char first[15]; printf("\n\tWrite here your code"); scanf("%s",first); if()//there is "n&quo

此代码将用字符
'1'
替换出现的字符
'n'

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

 int main()
 {
    char first[15];
    printf("\n\tWrite here your code");
    scanf("%s",first);
    if()//there is "n" in the char
    //change n with 1
    else
    //quit
    return 0;
  } 
用字符替换字符 此代码将用字符
'1'
替换出现的字符
'n'

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

 int main()
 {
    char first[15];
    printf("\n\tWrite here your code");
    scanf("%s",first);
    if()//there is "n" in the char
    //change n with 1
    else
    //quit
    return 0;
  } 
这样一个琐碎的问题应该得到一个非琐碎的答案 请注意,@Andy Sukowski Bang演示的这种分支方法——当使用
-O3
-编译时——比我的方法慢大约6.5倍,说明了分支指令上的位操作的效率(更不用说漏洞及其在启用缓解措施时的分支)

以下程序将每次出现的字母
转换为指定的字母

它能够将
'A'到'z'
'8'
之间的所有字符替换为
8286208字节的字符串,仅需0.07s:

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

#include <stdio.h>

int main(void)
{
    char str[15];
    printf("Input: ");
    scanf("%14s", str); // prevent buffer overflow

    char mod[] = "";

    for (int i = 0; str[i]; ++i) { // iterate over str
        if (str[i] == 'n') {
            char *repl = "1+k";
            strcat(mod, repl); // add "1+k" to mod
        } else {
            strncat(mod, &str[i], 1); // add str[i] to mod
        }
    }

    printf("Modified: %s\n", mod);

    return 0;
}
PS:没有使用malloc,我使用了VLA,这很糟糕,不要在家里这样做

这样一个琐碎的问题应该得到一个非琐碎的答案 请注意,@Andy Sukowski Bang演示的这种分支方法——当使用
-O3
-编译时——比我的方法慢大约6.5倍,说明了分支指令上的位操作的效率(更不用说漏洞及其在启用缓解措施时的分支)

以下程序将每次出现的字母
转换为指定的字母

它能够将
'A'到'z'
'8'
之间的所有字符替换为
8286208字节的字符串,仅需0.07s:

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

#include <stdio.h>

int main(void)
{
    char str[15];
    printf("Input: ");
    scanf("%14s", str); // prevent buffer overflow

    char mod[] = "";

    for (int i = 0; str[i]; ++i) { // iterate over str
        if (str[i] == 'n') {
            char *repl = "1+k";
            strcat(mod, repl); // add "1+k" to mod
        } else {
            strncat(mod, &str[i], 1); // add str[i] to mod
        }
    }

    printf("Modified: %s\n", mod);

    return 0;
}

PS:没有使用malloc,我使用了VLA,这很糟糕,不要在家里这样做

用于查找
'n'
scanf(“%s”,首先)最好是
scanf(“%14s”,第一个)
(或研究
fgets()
)以避免缓冲区溢出。“字符中的字母”没有意义,因为字符是字母juhász Koppány,如果
char
数组中存储的字符串中有多个
'n'
,该怎么办?如果有多个“n”,则每“n”更改一次使用1查找
'n'
scanf(“%s”,首先)最好是
scanf(“%14s”,第一个)
(或研究
fgets()
)以避免缓冲区溢出。“字符中的字母”没有意义,因为字符是字母Juhász Koppány,如果
char
数组中存储的字符串中有超过1个
'n'
,该怎么办?如果有超过1个“n”,则将每个“n”更改为1,我还可以打印f而不是1个“k+1”?@JuhászKoppány所以你不想用字符串
“k+1”
替换字符
'n'
?是的,我想用字符串“k+1”替换字符“n”@JuhászKoppány我现在已经更新了我的答案,包含了用字符串替换字符所需的代码。我可以用printf代替1“k+1”吗?@JuhászKoppány所以你不想用字符串
“k+1”
替换字符
'n'
?是的,我想用字符串“k+1”替换字符“n”@JuhászKoppány我现在更新了我的答案,包括用字符串替换字符所需的代码。
eq = !(*s ^ from); // eq will equal 0 if letters are same, else it will equal 1
*s = !eq * *s + eq * to; // we assign to the pointer its same old value with !eq if the character 'from' was absent, else we will assign the character 'to'.