Thread: concatenating (adding) two character strings

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    1

    Post concatenating (adding) two character strings

    I was told to write a program containing a concatenate function. This program should collect the input strings using fgets "(&s1[0], len1+1, stdin)"and then add the two to each other to produce a final product.
    My problem falls in that the program compiles but it doesn't display anything on the screen whatsoever, here's what I've got. I couldn't see how I could get it solved without this method of approach.

    Code:
    #include <stdio.h>
    #include <string.h>
    //function to terminate the program incase reach of 0
    int str_len (char s[])
    {
    int i=0;
    while (s[i]!= NULL)
    ++i;
    return i+1;
    }
    char string_cat (char*s1, char*s2)
    {
    //ADDING THE TWO STRINGS
    int str_len(char s[])
    char s1 [80]= {'0'};
    char s2 [40]= {'0'};
    int len1= (int)str_len(&s1);
    int  len2= (int) str_len(&s2);
    char *strcat (s1, s2);
    return;
    }
    int main ()
    {
    char string_cat(char*s1,char*s2)
    int str_len(char s[])
    //RECIVING THE STRINGS TO ADD
    char s1 [80];
    char s2 [40];
    int i=0;
    printf("What is the first sentence?: ")
    fgets(*s1[0], 35+1, stdin);
    printf("What is the second sentence?:")
    fgets(*s2[0],75+1,stdin);
    if (int i=0; len1+len2<80; ++i)
    {
    string_cat(*s1,*s2);
    else
        printf (“Sorry, too long of a sentence”);
    }
    printf("The two sentences added together produce the following: %c",s1[] )
    ++i
    return null;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    #include <string.h>
    
    //function to terminate the program incase reach of 0
    
    int str_len(char s[])
    {
        int i = 0;
        while (s[i] != NULL)
            ++i;
        return i + 1;
    }
    
    char string_cat(char *s1, char *s2)
    {
        //ADDING THE TWO STRINGS
        int str_len(char s[])
        char s1 [80]= {'0'};
        char s2 [40]= {'0'};
        int len1= (int)str_len(&s1);
        int  len2= (int) str_len(&s2);
        char *strcat (s1, s2);
        return;
    }
    
    int main()
    {
        char string_cat(char *s1, char *s2)
        int str_len(char s[])
        //RECIVING THE STRINGS TO ADD
        char s1 [80];
        char s2 [40];
        int i = 0;
        printf("What is the first sentence?: ")
        fgets(*s1[0], 35 + 1, stdin);
        printf("What is the second sentence?:")
        fgets(*s2[0], 75 + 1, stdin);
        if (int i = 0; len1 + len2 < 80; ++i)
        {
            string_cat(*s1, *s2);
            else
                printf("Sorry, too long of a sentence");
        }
        printf("The two sentences added together produce the following: %c", s1[])
        ++i
        return null;
    }
    Having indented your code a number of mistakes become evident.

    For str_len:
    • The parameter should be: const char s[] since str_len does not modify the string.
    • NULL is a null pointer constant. You should use '\0' for a null character.
    • Returning i + 1 looks wrong unless your intention is to compute string length including the null character, unlike the standard strlen.

    For string_cat:
    • You appear to have function declarations that are out of place.
    • You need to be clear about the types, e.g., s1 is a char*, so &s1 is a char**, therefore &s1 is not a valid argument to str_len.
    • Why is string_cat declared as returning a char, and why do you return nothing?
    • You don't seem to have actually attempted to implement the concatenation itself.

    For main:
    • Once again, unnecessary function declarations.
    • You keep forgetting to terminate with semi-colons.
    • Once again, be careful as to type: s1 is a char*, so s1[0] is a char, hence *s1[0] does not make sense.
    • Notice that your if statement is mismatched with its else branch.
    • Your printf statement is incorrect: s1[] is simply invalid syntax, and since you want to print a string, you should use %s not %c.
    • null is not a valid identifier. You should just return 0;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  2. concatenating strings?
    By liri in forum C Programming
    Replies: 11
    Last Post: 08-24-2007, 05:34 PM
  3. Concatenating 2 strings
    By AngKar in forum C Programming
    Replies: 14
    Last Post: 04-25-2006, 10:51 AM
  4. Adding character arrays to strings.
    By w00tw00tkab00t in forum C++ Programming
    Replies: 28
    Last Post: 02-06-2006, 07:03 PM
  5. concatenating more than 2 strings
    By Grunt12 in forum C Programming
    Replies: 3
    Last Post: 11-25-2005, 05:31 PM

Tags for this Thread