C用一个字符数组替换另一个字符 [英] C Replace one character in an char array by another

查看:251
本文介绍了C用一个字符数组替换另一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下C代码

  #include< stdio.h> 
int main(void){
char c [] =ABC
printf(%s,c);
c [1] ='B';
printf(%s,c);
return 0;
}

我想要的输出是 ABC BBC 但我得到的输出是 ABC ABC 。如何替换String / char数组中的第一个字符?

解决方案

C数组中的索引从 0 。所以你必须用 c [0] ='B'替换 c [1] ='B' / p>

另请参阅今天类似的问题:输出C ++中的微笑 - 我已经在其中提供了更详细的说明:)


I have the following C Code

#include <stdio.h>
int main(void){
    char c[] = "ABC"
    printf("%s ", c);
    c[1] = 'B';
    printf("%s", c);
    return 0;
}

The output I want is ABC BBC but the output I get is ABC ABC. How can I replace the first character in an String / char array?

解决方案

Indexing in C arrays start from 0. So you have to replace c[1] = 'B' with c[0] = 'B'.

Also, see similar question from today: Smiles in output C++ - I've put a more detailed description there :)

这篇关于C用一个字符数组替换另一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆