Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C字符串格式_C_String_Formatting - Fatal编程技术网

C字符串格式

C字符串格式,c,string,formatting,C,String,Formatting,我写了一个程序来解决这个问题。“编写一个程序,给定一个字符串、一个宽度和一个空字符串作为输出,将该字符串置于输出区域的中心。使用一个函数,如果格式化成功,则返回1;如果有任何错误,如字符串大于长度,则返回0。”。我的问题是,My程序在打印字符串时只返回大量的奇数字符。而且它不会标记0。我可以做些什么来修复代码,或者更好地解决问题 完整代码: #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h

我写了一个程序来解决这个问题。“编写一个程序,给定一个字符串、一个宽度和一个空字符串作为输出,将该字符串置于输出区域的中心。使用一个函数,如果格式化成功,则返回1;如果有任何错误,如字符串大于长度,则返回0。”。我的问题是,My程序在打印字符串时只返回大量的奇数字符。而且它不会标记0。我可以做些什么来修复代码,或者更好地解决问题

完整代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int formatString(char str[250], char strcopy[250], int width);

int main()
{
    char str[250];
    char strcopy[250];
    int width;
    int outcome;

    printf("Enter some text:\n");
    gets(str);

    printf("\nEnter a width (to check): ");
    scanf("%d", &width);
    printf("What you entered:");
    printf("| %s |\n", str);
    printf("\n");

    outcome = formatString(str, strcopy, width);

    if (outcome = 1)
    {
        printf("String copied.\n");
        printf("| %s |", strcopy);
    }
    else
    {
        printf("Use a width val. that is the length of string\n");
    }

    return 0;
}

int formatString(char str[250], char strcopy[250], int  width)
{
    int sapceCheck;
    int temp = 0;

    sapceCheck = width - 1;

    for (int i = 0; i < width; i++)
    {
        if (str[i] == '\0')
        {
            printf("Formating sucessful\n");
            strncpy(str, strcopy, sizeof(str)-1); * (str + (sizeof(str) - 1)) = '\0';
            temp = 1;
        }
    }

    if (temp == 0)
    {
        return 0;
    }
    else
    {
        printf("Formating not sucessful\n");
        printf("Width does not work\n");
        return 0;
    }
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括
#包括
int-formatString(字符str[250],字符strcopy[250],int-width);
int main()
{
char-str[250];
字符结构副本[250];
整数宽度;
int结果;
printf(“输入一些文本:\n”);
获取(str);
printf(“\n输入宽度(要检查):”;
扫描频率(“%d”和宽度);
printf(“您输入的内容:”);
printf(“|%s |\n”,str);
printf(“\n”);
结果=格式字符串(str、strcopy、宽度);
如果(结果=1)
{
printf(“字符串已复制。\n”);
printf(“|%s |”,strcopy);
}
其他的
{
printf(“使用宽度值,即字符串的长度\n”);
}
返回0;
}
int-formatString(字符str[250],字符strcopy[250],int-width)
{
int sapceCheck;
内部温度=0;
sapceCheck=宽度-1;
对于(int i=0;i
关于您的问题的旁注:成功时返回1,失败时返回0不是C文献中的标准函数行为(除了返回布尔值的函数之外)。你是在学习指南还是课程

您不应该任意为字符串预分配250个字符。字符串通常由字符指针表示,通过动态内存分配进行处理。我知道get()需要预先分配的缓冲区,但这是一种非常糟糕的做法。您应该考虑使用fgss-()函数,并使用其最大字符数参数来获得更好的安全性。硬编码字符串长度也很难维护(需要在多个位置修改长度),读者也会感到困惑(不清楚编译器是通过复制还是引用传递参数)

另外,不要试图隐藏代码。包含
strncpy()
的行有两条语句,一元星挂起在两条语句之间,看起来像一个乘法运算符。不要做这种事。将第二句话换一行

也就是说,您还误解了
strncpy
函数的参数:它首先是目标,然后是源。将未初始化的缓冲区(strcopy)复制到字符串中。请注意,您还误解了
temp
变量的值,该变量在运行正常时返回错误。它还返回0,而不是按要求返回1

最后一点注意:你真的应该
break函数
formatString()
中的for循环,否则将对字符串中的每个
\0
执行strncpy


如果你是自学的,我建议你在书上检查练习,如果你正在学习课程,我建议你向助教寻求帮助。他们将能够指导您正确理解错误。

请不要错过“Soravux”发布的答案,其中包含关于如何修复“问题”代码的所有正确建议

这是另一种方法。但是,调用方必须确保目标字符串“strcpy”足够大(长度+1)以容纳输出:

int formatString(char *str, char *strcopy, int length)
   {
   size_t strLength;
   strLength = strlen(str);

   /* Check if the string is greater than the length */
   if(strLength > length)
      return(0);

   /* Print the centered 'str' to 'strcopy'. */
   sprintf(strcopy, "%*s%*s",
      (length+strLength) / 2, str,           //leading spaces & 'str'
      length - ((length+strLength) / 2), ""  //trailing spaces
      );

   return(1);
   }
阅读
sizeof()
strlen()
。还要添加一些
printf
s来查看它们实际运行的内容。交替地用符号编译代码,并使用调试器运行程序代码,逐行跟踪并检查所有相关变量。并且
if(output=1)
应该是
if(output=1)