C 程序复制字符串而不使用具有足够内存的 strcpy()



我正在尝试有这个输出:

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

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

这就是我对康卡特方法的

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

长度法为:

 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 ''                                   
      // 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: %dn", 
             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' 执行不好吗?

代码中存在多个问题:

  • length函数中的循环测试不正确:i < str[i]它应该是:

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

     for (j = 0; src[j] != ''; 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] != ''; j++) {
        dest[len + j] = src[j];
    }
    dest[len + j] = '';
}
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: %dn",
           str4, str6, strcmp(str4, str6));
    return 0;
}

您在这两个函数中都有几个问题:

concat

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

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

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

这里你添加的数据偏移量为 i ,但 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] != ''; j++) { 
      dest[len_dst+j] = src[j];
    }
    dest[len_dst+j] = '';
}

length

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

同样的话,这个退出条件是什么? src[i] != ''就够

因此,更正后的函数可能是:

 int length(char str[])
 {
      int len=0;
      int i;
      /* clear exit condition */
      for ( i=0; str[i] != ''; 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);

最新更新