You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言打印ASCII值时如何将空格的32替换为空格字符输出

替换ASCII输出中空格对应32为空格字符的修改方案

你只需要在循环中增加字符判断逻辑,遇到空格字符时直接输出空格,其余字符仍输出对应ASCII值即可。

修改后的完整代码

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

int main (void) {
    string text = get_string("plaintext:");
    for (int i=0; text[i] !='\0'; i++) {
        if (text[i] == ' ') {
            // 替换原32的输出为空格,保留原有分隔逻辑
            printf("  ");
        } else {
            int s = (int) text[i];
            printf("%i ", s);
        }
    }
    printf("\n");
}

修改说明

  • 新增空格判断分支:当text[i]等于空格字符' '时,直接输出空格替换原32的数值输出
  • 非空格字符保持原有逻辑,输出对应十进制ASCII值
  • 输出间隔和原有逻辑完全一致,每个输出项后保留一个空格做分隔

运行效果示例

输入this is a string时,输出如下:

./scrabbleee
plaintext:this is a string
116 104 105 115  105 115  97  115 116 114 105 110 103 

内容的提问来源于stack exchange,提问作者haseen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 11:18:00