【问题标题】:C program copy string without using strcpy() with enough memoryC程序复制字符串而不使用具有足够内存的strcpy()
【发布时间】:2017-10-25 05:10:16
【问题描述】:

我正在尝试这个输出:

Comparing results of concat and strcat ...
strcmp("Plain old stringTroy", "Plain old stringTroy") says: 0

如果两个字符串参数相同,则 strcmp 返回 0。如果结果为 0,则 concat 的行为与库函数 strcat 完全相同。

这就是我所拥有的 concat 方法。

#define MAXSIZE 32       
void concat(char dest[], char src[])                                        
{                                                                           
    int i=length(src);                                                          
    int j=0;                                                                    
    for(j; j<src[j] !='\0'; j++) {                                                                           
      dest[i+j] = src[j];                                                       
    }                                                                           
    dest[i+j] = '\0';                                                        
} 

长度方法是:

 int length(char str[])                                                      
 {                                                                           
      // Add code here to return the length of the                              
      // string str without using the strlen function                           
      // Do not count the null character '\0'                                   
      // in computing the length of the string                                  
      int len=0;                                                                
      int i;                                                                    
      for(i=0;i<str[i];i++) {                                                    
          len++;                                                                  
      }                                                                         
      return len;                                                               
 }  

这是我的主线

int main()                                                                  
{                                                                           
      // Variable declarations for all parts        
      char str2[] = "Troy";                                                                                                
      char str4[] = "Plain old string";                                         
      char str6[MAXSIZE];   
   // Part 6                                                                 
      printf("\n----- Part 6 -----\n");                                         
      // Make a copy of the destination string first, to be reused later        
      strcpy(str6, str4);                                                       
      concat(str4, str2);                                                       
      strcat(str6, str2);                                                       
      printf("Comparing results of concat and strcat ...\n");                   
      printf("strcmp(\"%s\", \"%s\") says: %d\n", 
             str4, str6, strcmp(str4, str6)
            );   

      return 0;                                                                 
}

这是我运行时的输出:

----- Part 6 -----
Comparing results of concat and strcat ...
strcmp("PlaiTroy", "Plain old stringTroy") says: -1

第一个字符串与第二个字符串不同,这就是我得到 -1 的原因。我的问题出在我的 concat 方法中,但我似乎无法理解为什么它不能很好地执行。是因为空格吗? 0 和 '\0' 执行不好吗?

【问题讨论】:

  • j&lt;src[j] !='\0' 这是一个奇怪的情况。不就是src[j] !='\0'
  • 也在你的 len 方法中 i&lt;str[i] ???应该是for(i=0;str[i];i++)
  • 数组 str4 有 17 个字符的空间,包括终止符。想想当您使用str4 作为目标字符串时,concat 函数中会发生什么。
  • @MillieSmith 你不必说服 代码是错误的 :)
  • @Someprogrammerdude 我们也不知道MAXSIZE 是否足够大。

标签: c strcpy


【解决方案1】:

您的代码中存在多个问题:

  • length函数中的循环测试不正确:而不是i &lt; str[i],应该是:

     for (i = 0; str[i] != '\0'; i++)
    
  • concat 函数中的相同问题。将循环更改为:

     for (j = 0; src[j] != '\0'; j++) {
    
  • 同样在concat函数中,i应该是dst的长度,而不是src的长度。您可以使用 len 而不是 i 作为此变量。

  • 函数main 中的数组str4 在末尾没有任何空间可供concat 追加任何内容。以这种方式将其定义为更大的尺寸:

    char str4[MAXSIZE] = "Plain old string";      
    

这里是更正的版本:

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

#define MAXSIZE 32

void concat(char dest[], char src[]) {
    int len = length(dest);
    int j;
    for (j = 0; src[j] != '\0'; j++) {
        dest[len + j] = src[j];
    }
    dest[len + j] = '\0';
}

int length(char str[]) {
    int len = 0;
    int i;
    for (i = 0; i < str[i]; i++) {
        len++;
    }
    return len;
}

int main(void) {
    // Variable declarations for all parts
    char str2[MAXSIZE] = "Troy";
    char str4[MAXSIZE] = "Plain old string";
    char str6[MAXSIZE];
    // Part 6
    printf("\n----- Part 6 -----\n");
    // Make a copy of the destination string first, to be reused later
    strcpy(str6, str4);
    concat(str4, str2);
    strcat(str6, str2);
    printf("Comparing results of concat and strcat ...\n");
    printf("strcmp(\"%s\", \"%s\") says: %d\n",
           str4, str6, strcmp(str4, str6));
    return 0;
}

【讨论】:

    【解决方案2】:

    这两个函数都有几个问题:

    concat

    for(j; j<src[j] !='\0'; j++) {
    

    这里的退出条件是什么?src[j] != '\0'就够了。

    dest[i+j] = src[j];                                                       
    

    在这里添加偏移量为i 的数据,但我是src 的长度,而不是dst

    所以修正后的函数可能是:

    void concat(char dest[], char src[])
    {
        /* descriptive variable name */
        int len_dst = length(dst);
        int j=0;
    
        /* clear exit condition */
        for(; src[j] != '\0'; j++) { 
          dest[len_dst+j] = src[j];
        }
        dest[len_dst+j] = '\0';
    }
    

    length

    for(i=0;i<str[i];i++) {  
    

    同样的说法,这个退出条件是什么? src[i] != '\0'就够了

    所以修正后的函数可能是:

     int length(char str[])
     {
          int len=0;
          int i;
          /* clear exit condition */
          for ( i=0; str[i] != '\0'; i++) {
              len++;
          }
          return len;
     }
    

    main

    main 函数中的警告:

    char str4[] = "Plain old string";
    concat(str4, str2);  /* <- erases what is after str4 */
    

    您没有足够的空间来存储结果。像这样写:

    char str2[] = "Troy";
    char str4[MAXSIZE] = "Plain old string"; /* <-- reserve spaces after str4*/
    /* ... */
    concat(str4, str2);
    

    【讨论】:

      猜你喜欢
      • 2019-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多