Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7495303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:01:26+00:00 2026-05-29T18:01:26+00:00

I’m learning C language… I want to write a function that concatenates two strings.

  • 0

I’m learning C language…

I want to write a function that concatenates two strings.
I wrote a function but it doesn’t work;
it doesn’t give any error in compilation,
but while running it doesn’t do anything.

Here is my code:

char* str_sum(char* s1, char* s2){
    int j = strlen(s1);
    int i=0;
    while(s2[i]){
            s1[j]=s2[i];
            j++;
            i++;
    }
    return s1;
}
int main(){
    char* s1, s2;
    s1 = "Joe";
    s2 = "Black";
    printf("%s\n",sum_str(s1,s2));
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:01:28+00:00Added an answer on May 29, 2026 at 6:01 pm

    Your function could look like this:

    char* sum_str(char* s1, char* s2)
    {
        int lenS1 = strlen(s1);
        int lenS2 = strlen(s2);
        char* newString = malloc((lenS1 + lenS2 + 1) * sizeof(char));
        int i = 0;
        while(i < lenS1)
        {
            newString[i] = s1[i];
            i++;
        }
        while(i < lenS2 + lenS1)
        {
            newString[i] = s2[i - lenS1];
            i++;
        }
        newString[i] = '\0';
        return newString;
    }
    

    Note that this function allocates new string which means you should free this data when you finish with this string. Also note that terminating character ('\0') is stored at the end of this char array so that printf can “print” it properly.

    Here’s main:

    int main()
    {
      char *s1, *s2, *s3;
      s1 = "Joe";
      s2 = " Black";
      s3 = sum_str(s1,s2);
      printf("%s\n", s3);
      free(s3);
      return 0;
    }
    

    Output: Joe Black

    Note that I have declared variables s1, s2 and s3 like this: char *s1, *s2, *s3;. If I write it like this: char *s1, s2, s3; then variables s2 and s3 are no longer arrays of characters but only characters.

    Also note that this program:

      char *s1 = "Joe";
      s1[0] = 'X';
      printf("%s\n", s1);
    

    will crash since it tries to change constant string literal "Joe". s1 is pointer to first character of this literal in this case.

    But this program will work fine and its output will be Xoe:

      char s1[] = "Joe";
      s1[0] = 'X';
      printf("%s\n", s1);
    

    s1 is an array initialized with string "Joe" so it’s OK to change it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.