c - 即使在调试过程中看起来正确,也无法打印字符串
问题描述
我一直在尝试编写一个程序,它将字符串中的所有字母替换为它们在字母表中的位置。我想出了一个代码,它似乎在调试期间工作,但由于某种原因它不会打印结果字符串。
在调试过程中 main 中的字符串变量似乎是正确的,但它仍然不会打印。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char *alphabet_position(char *text) {
char string[100] = {};
char temp_c[5] = {};
int i, temp_i;
for (i=0; i<strlen(text); i++){
if (text[i] >= 65 && text[i] <= 90){
temp_i = text[i] - 'A' + 1;
snprintf(temp_c, 4, "%d ", temp_i);
strcat(string, temp_c);
}
if (text[i] >= 97 && text[i] <= 122){
temp_i = text[i] - 'a' + 1 ;
snprintf(temp_c, 4, "%d ", temp_i);
strcat(string, temp_c);
}
}
return string;
}
int main()
{
char *string = "The sunset sets at twelve o' clock.";
char *temp = alphabet_position(string);
printf("%s" ,temp);
return 0;
}
另外,我真的不明白为什么在 strcat 调用期间使用 char *string 而不是 char string[100] 会导致分段错误。
解决方案
推荐阅读
- c# - JObject.Load JsonReaderException
- java - 流到 LinkedHashSet
- c# - 为什么在调用者而不是在有问题的行上引发异常
- c# - 静态资源与 TabControl 中的 ItemSource 混合
- java - 当 module-info.java 存在时 findResource("") 返回 null,这是为什么呢?
- microsoft-edge - 是否可以在 Microsoft Edge 上禁用同源策略?
- php - 如何创建从 php 发送的响应式电子邮件?
- go - 如何从 Golang 中的 dialoflow webhook 中踢出 FollowupEvent
- mysql - 减去 MySQL 中 2 个特定行的值
- windows-10 - Docker:运行使用依赖项构建的图像时出现超时错误