Yvette
Yvette

Reputation: 83

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

I am trying to have this output:

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

The strcmp returns 0 if the two string arguments are identical. If the result is 0, then the concat behaves exactly like the library function strcat.

this is what I have for concat method.

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

length method is:

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

This is my main

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

This is my output when I run it:

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

The first string is not the same as the second string which is why I am getting a -1. My problem is in my concat method but I can't seem to understand why it won't execute well. Is it because of the spaces? Is 0 and '\0' not executing well?

Upvotes: 1

Views: 1406

Answers (2)

chqrlie
chqrlie

Reputation: 145277

There are multiple problems in your code:

  • The loop test in the length function is incorrect: instead of i < str[i], it should be:

     for (i = 0; str[i] != '\0'; i++)
    
  • the same problem in the concat function. Change the loop to:

     for (j = 0; src[j] != '\0'; j++) {
    
  • also in the concat function, i should be the length of dst, not that of src. You might use len instead of i for this variable.

  • The array str4 in function main does not have any space available at the end for concat to append anything. Define it with a larger size this way:

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

Here is the corrected version:

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

Upvotes: 2

Mathieu
Mathieu

Reputation: 9659

You have several problems in both functions:

concat

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

What is the for exit condition here?, src[j] != '\0' is enough.

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

Here you add data with an offset of i, but I is the length of src, not dst.

So the corrected function could be:

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++) {  

Same remark, what is this exit condition? src[i] != '\0' is enough

So the corrected function could be:

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

main

And warning in main function:

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

You do not have enough space to store result. Write something like:

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

Upvotes: 1

Related Questions