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

C program copy string without using strcpy() with enough memory

我试图得到这个输出:

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

strcmp returns 如果两个字符串参数相同则为 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] !='[=11=]'; j++) {                                                                           
      dest[i+j] = src[j];                                                       
    }                                                                           
    dest[i+j] = '[=11=]';                                                        
} 

长度方法是:

 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 '[=12=]'                                   
      // 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'是不是执行的不好?

你的两个函数都有几个问题:

concat

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

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

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] != '[=12=]'; j++) { 
      dest[len_dst+j] = src[j];
    }
    dest[len_dst+j] = '[=12=]';
}

length

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

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

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

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

main

并在 main 函数中发出警告:

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

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

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

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

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

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

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

  • 函数 main 中的数组 str4 末尾没有任何 space 可供 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] != '[=13=]'; j++) {
        dest[len + j] = src[j];
    }
    dest[len + j] = '[=13=]';
}

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;
}