Hey guys im having trouble writing this code for my function. I have the idea down and a way to solve the problem but im having trouble writing it in C code. Basically im writing a function that runs exactly like the function strcat from the string.h library but we cant use it so we have to make our own. We were given a header file and we were suppose to write the functions that were in the header file. This is what i got so you guys can see whats happening:

code for strfuncs.c :

#include <stdio.h>
#include "strfuncs.h"
#define ONE_HUNDRED 100

int strlen(const char *s) {
  /* Declarations */
  int x=0;

  /* Read in characters until null */
  while (*s != '\0') {
    x++;
    s++;
  }
  
  return(x);
}
char* strcat(char *to, const char* from) {
  /* Declarations */
  int length_1,length_2,i;
  char *str3;

  
  /* Get the length of each string */
  length_1 = strlen(to);
  length_2 = strlen(from);

  for (i=0;i < length_1;i++) {
     str3[i] = to[i];
  }

  for (i=0;i < length_2;i++) {
    str3[length_1+i] = from[i];
  }

  return str3;
}

header file: 
#ifndef STR_FUNCS_H
#define STR_FUNCS_H

int strlen(const char *str);
char *strcat(char *to, const char *from);
char* modifystr(char *str, char from, char to);

#endif
test file: #include <stdio.h>
#include "strfuncs.h"
#define ONE_HUNDRED 100

int main () {
  /* Get user inputs */
  char first_choice[ONE_HUNDRED], second_choice[ONE_HUNDRED];
	int first_length,second_length;
 
  /* Begin execution */
  printf("Enter string one: ");
  scanf("%s", first_choice);
  printf("Enter string two: ");
  scanf("%s", second_choice);
  
  /* Calculate the lengths of the strings and print out */
  first_length = strlen(first_choice);
  second_length = strlen(second_choice);
  printf("String one is %d characters long\n",first_length);
  printf("String two is %d characters long\n",second_length);
  printf("The concatenated string is: %d", strcat(first_choice,second_choice)); 
  

  return 0;
}

As you can see i also added the test file to help you understand what im doing. At compile time when i enter the following compile code i get this error message:
gcc -ansi -Wall -Werror -lm strfuncs.c strtester.c
cc1: warnings being treated as errors
strfuncs.c: In function ‘strcat’:
strfuncs.c:41: error: function returns address of local variable

do you guys any suggestions how i could return the arrays contents???

A local variable (str3) is deleted once the function ends.

First, loop through the to string to find the \0.
Second, now loop through from copying each character to to.
Return to.

speedy94519> do you guys any suggestions how i could return the arrays contents???
Yes, make str3 to point to the destination. Work with that pointer and then return destination.

char* strcat(char *to, const char* from) {
    /* Declarations */
    int length_1,length_2,i;
    char *str3;


    /* Get the length of each string */
    length_1 = strlen_again(to);
    length_2 = strlen_again(from);

    /*
     * point to the destination
     */
    str3 = to;

    /*
     * since you know already the length of the content
     * on destination, use it to start there
     */
    str3 = str3 + length_1;

    for (i=0;i < length_2;i++) {
        *str3 = from[i]; /* copy next char into destination */
        ++str3; /* advance the start point */
    }
    *str3 = '\0'; /* don't forget to make it a string */

    return to; /* to points still to the beginning of destination */
}

Corrected some of your code to show you a possible way.

By the way:

printf("The concatenated string is: %d", strcat(first_choice,second_choice));

Will not display what is intended. But...

printf("The concatenated string is: %d", strlen(strcat(first_choice,second_choice)));

will.

A local variable (str3) is deleted once the function ends.

First, loop through the to string to find the \0.
Second, now loop through from copying each character to to.
Return to.

I see thanks for responding. I actually solved it now :) .

speedy94519> do you guys any suggestions how i could return the arrays contents???
Yes, make str3 to point to the destination. Work with that pointer and then return destination.

char* strcat(char *to, const char* from) {
    /* Declarations */
    int length_1,length_2,i;
    char *str3;


    /* Get the length of each string */
    length_1 = strlen_again(to);
    length_2 = strlen_again(from);

    /*
     * point to the destination
     */
    str3 = to;

    /*
     * since you know already the length of the content
     * on destination, use it to start there
     */
    str3 = str3 + length_1;

    for (i=0;i < length_2;i++) {
        *str3 = from[i]; /* copy next char into destination */
        ++str3; /* advance the start point */
    }
    *str3 = '\0'; /* don't forget to make it a string */

    return to; /* to points still to the beginning of destination */
}

Corrected some of your code to show you a possible way.

By the way:

printf("The concatenated string is: %d", strcat(first_choice,second_choice));

Will not display what is intended. But...

printf("The concatenated string is: %d", strlen(strcat(first_choice,second_choice)));

will.

thank you

thank you

Part of the functionality of a function relies on the ability to perform a task as independently as possible.
If you notice, your strcat() depends on, and calls strlen(), which is external to it.

A more functional strcat() would call for the use of not such external sources.
Can you think of a way of making it so?

Hello Friends!

I think there is confusion on the strcat function to make it work correct.

Suppose you want to take a string and add it onto the end of another string.

For example, you have 2 strings, "abcd" and "efgh", and you want to join them into one string, "abcdefgh".

The function you use for this is strcat(string1,string2). It takes string2 and appends it onto the end of string1.

Consider the following sample code:

char str1[1000];
char * str2 = "test of strcat";

strcpy(str1,"this is a ");
strcat(str1,str2);

printf("Result: %s\n",str1);

What is going on here?

First off we are allocating space for the first string, 1000 characters max.

Next, we define another string, str2, using a character pointer to a constant string, "test of strcat".

We initialize str1 using the string copy function, strcpy.

At this point str1 contains the string 'this is a '. The last character is the null character at position 10, after the 'a' and the final space.

Finally we get to our string concatenation function, strcat.

Strcat starts at the null at position 10, and replaces it with the first 't' in "test of strcat".

It continues putting characters in the array until it gets to the end of " of strcat", at which point it puts a terminating null after the 't', marking the new end of the string.

IMPORTANT NOTE: The original string, the first parameter in the strcat() function, must be large enough to contain the final string. If I had allocated the original string to be 10 characters, the strcat might cause a run-time error (segmentation fault), as the program attempts to write past the end of the array.

I hope this finds everybody feeling happy and well! God Bless!! :) :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.