C 字符串的Printf不情愿地向下移动了一行
这是我的程序(学校练习,应该从用户处接收字符串,更改它并以特定格式返回原始字符串和新字符串):C 字符串的Printf不情愿地向下移动了一行,c,printf,C,Printf,这是我的程序(学校练习,应该从用户处接收字符串,更改它并以特定格式返回原始字符串和新字符串): #包括 #定义最大字符串长度50 字符开关字符(字符c){ 如果((c>='A')&&(c='A')&&(c'5')&&(c='0')&&(c”); printf(“\%s\”,新闻字符串); 返回0; } 运行时,输出在字符串之后和最后一个“字符之前向下一行,尽管它应该全部打印在同一行中 如果有任何指示,我将不胜感激。这是因为fgets()也会读取换行符,前提是缓冲区中有空间,并且它存储在您的新闻
#包括
#定义最大字符串长度50
字符开关字符(字符c){
如果((c>='A')&&(c='A')&&(c'5')&&(c='0')&&(c<'5')){
c=48;
}
返回c;
}
int main(void){
焦炭温度;
int i=0;
char stringInput[最大字符串长度+1];
printf(“请输入有效字符串\n”);
fgets(stringInput,50,标准输入);
char newString[最大字符串长度+1];
而((i!=MAX_STRING_LENGTH+1)和&(stringInput[i]!='\0')){
温度=开关字符(stringInput[j]);
新闻字符串[i]=临时;
i++;
}
printf(“\%s\”,stringInput);
printf(“->”);
printf(“\%s\”,新闻字符串);
返回0;
}
运行时,输出在字符串之后和最后一个“
字符之前向下一行,尽管它应该全部打印在同一行中
如果有任何指示,我将不胜感激。这是因为fgets()
也会读取换行符,前提是缓冲区中有空间,并且它存储在您的新闻字符串中
您可以通过以下操作将其删除:
fgets(stringInput,50,stdin);
stringInput[strcspn(stringInput, "\n")] = 0; /* removes the trailing newline if any */
发件人:
fgets()从流中最多读取一个小于大小的字符
并将其存储到s指向的缓冲区中。读取在
EOF或换行符。如果读取换行符,则将其存储到
缓冲区。在缓冲区中最后一个字符之后存储终止的空字节('\0')
这是因为fgets()
如果缓冲区中有空间并且它存储在新闻字符串中,那么它也会读取换行符
您可以通过以下操作将其删除:
fgets(stringInput,50,stdin);
stringInput[strcspn(stringInput, "\n")] = 0; /* removes the trailing newline if any */
发件人:
fgets()从流中最多读取一个小于大小的字符
并将其存储到s指向的缓冲区中。读取在
EOF或换行符。如果读取换行符,则将其存储到
缓冲区。在缓冲区中最后一个字符之后存储终止的空字节('\0')
您的代码中有几个问题:
fgets()
读取并将换行符保留在目标数组的末尾(如果存在并且有足够的空间)。为了与算法保持一致,您应该剥离此换行符。您可以使用stringInput[strcspn(stringInput,“\n”)]安全地执行此操作='\0';
,如果不能使用
,请使用多一点的代码。此换行符的存在解释了观察到的不良行为
- 你用
fgets()读了一行
,但传递的缓冲区大小可能不正确:当数组大小为MAX\u STRING\u LENGTH+1
时,硬编码为50
。将MAX\u STRING\u LENGTH
定义为50
,这不是问题,但如果以后更改宏的定义,可能会忘记将size参数更新为fgets()
。使用stringInput的大小保持一致
- 您忘记在
newString
中设置空终止符。由于stringInput
在数组边界内以空终止,因此无需测试i
的边界值
- 在
switchChar()
中,不应硬编码ASCII字符集中的字符值:这会降低可移植性,最重要的是,会降低可读性
以下是一个经过更正和简化的版本:
#include <stdio.h>
#define MAX_STRING_LENGTH 50
char switchChar(char c) {
if ((c >= 'A') && (c <= 'Z')) {
c = c + ('a' - 'A');
} else
if ((c >= 'a') && (c <= 'z')) {
c = c - ('a' - 'A');
} else
if ((c > '5') && (c <= '9')) {
c = '8';
} else
if ((c >= '0') && (c < '5')) {
c = '0';
}
return c;
}
int main(void) {
char stringInput[MAX_STRING_LENGTH + 1];
char newString[MAX_STRING_LENGTH + 1];
int c;
printf("Please enter a valid string\n");
if (fgets(stringInput, sizeof stringInput, stdin) != NULL) {
// strip the newline character if present
//stringInput[strcspn(stringInput, "\n")] = '\0';
char *p;
for (p = stringInput; *p != '\0' && *p != '\n'); p++)
continue;
*p = '\0';
for (i = 0; stringInput[i] != '\0'; i++) {
newString[i] = switchChar(stringInput[i]);
}
newString[i] = '\0';
printf("\"%s\"", stringInput);
printf("->");
printf("\"%s\"", newString);
printf("\n");
}
return 0;
}
#包括
#定义最大字符串长度50
字符开关字符(字符c){
如果((c>='A')&&(c='A')&&(c'5')&&(c='0')&&(c<'5')){
c='0';
}
返回c;
}
int main(void){
char stringInput[最大字符串长度+1];
char newString[最大字符串长度+1];
INTC;
printf(“请输入有效字符串\n”);
if(fgets(stringInput,sizeof stringInput,stdin)!=NULL){
//如果存在换行符,请去掉换行符
//stringInput[strcspn(stringInput,“\n”)]='\0';
char*p;
对于(p=stringInput;*p!='\0'&&&&p!='\n');p++)
持续
*p='\0';
对于(i=0;stringInput[i]!='\0';i++){
newString[i]=switchChar(stringInput[i]);
}
新闻字符串[i]='\0';
printf(“\%s\”,stringInput);
printf(“->”);
printf(“\%s\”,新闻字符串);
printf(“\n”);
}
返回0;
}
您的代码中有几个问题:
fgets()
读取并将换行符保留在目标数组的末尾(如果存在并且有足够的空间)。为了与算法保持一致,您应该剥离此换行符。您可以使用stringInput[strcspn(stringInput,“\n”)]安全地执行此操作='\0';
,如果不能使用
,请使用多一点的代码。此换行符的存在解释了观察到的不良行为
- 你用
fgets()读了一行
,但传递的缓冲区大小可能不正确:当数组大小为MAX\u STRING\u LENGTH+1
时,硬编码为50
。将MAX\u STRING\u LENGTH
定义为50
,这不是问题,但如果以后更改宏的定义,可能会忘记将size参数更新为fgets()
。使用stringInput的大小保持一致
- 您忘记在
newString
中设置空终止符。由于stringInput
在数组边界内以空终止,因此无需测试i
的边界值
- 在
switchChar()
中,不应硬编码ASCII字符集中的字符值:这会降低可移植性,最重要的是,会降低可读性
以下是一个经过更正和简化的版本:
#include <stdio.h>
#define MAX_STRING_LENGTH 50
char switchChar(char c) {
if ((c >= 'A') && (c <= 'Z')) {
c = c + ('a' - 'A');
} else
if ((c >= 'a') && (c <= 'z')) {
c = c - ('a' - 'A');
} else
if ((c > '5') && (c <= '9')) {
c = '8';
} else
if ((c >= '0') && (c < '5')) {
c = '0';
}
return c;
}
int main(void) {
char stringInput[MAX_STRING_LENGTH + 1];
char newString[MAX_STRING_LENGTH + 1];
int c;
printf("Please enter a valid string\n");
if (fgets(stringInput, sizeof stringInput, stdin) != NULL) {
// strip the newline character if present
//stringInput[strcspn(stringInput, "\n")] = '\0';
char *p;
for (p = stringInput; *p != '\0' && *p != '\n'); p++)
continue;
*p = '\0';
for (i = 0; stringInput[i] != '\0'; i++) {
newString[i] = switchChar(stringInput[i]);
}
newString[i] = '\0';
printf("\"%s\"", stringInput);
printf("->");
printf("\"%s\"", newString);
printf("\n");
}
return 0;
}
#包括
#定义最大字符串长度50
字符开关字符(字符c){
如果((c>='A')&&
i = scanf("%50s", stringInput);
if (i != 1) { /* always control input function return code */
perror("Could not get input string");
return 1;
}
if (NULL == fgets(stringInput, 50, stdin)) { /* control input */
perror("Could not get input string");
return 1;
}
int l = strlen(stringInput);
if ((l > 0) && (stringInput[l - 1] == '\n')) { /* test for a trailing newline */
stringInput[l - 1] = '\0'; /* remove it if found */
}