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

  • SEARCH
  • Home
  • 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 7998707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:17:12+00:00 2026-06-04T15:17:12+00:00

The code was to copy and concatenate strings widout using library functions.the code i

  • 0

The code was to copy and concatenate strings widout using library functions.the code i used:

#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 100
char* newStrCpy(char string1[],char string2[])
{
        int i=0;
        while(string1[i]!='\0')
        {
            string2[i]=string1[i];
            i++;
        }
return (string2);
}
char* newStrCat(char destination[],char arr[])
{
int a=0;
int destlen=0;
while(destination[destlen]!='\0')
{
    destlen ++;
}
while(a<(MAX-destlen))
{
    destination[(destlen+a)]=arr[a];
    a++;
}
return destination;
}
void main()
 {
char string1[MAX],string2[MAX];
int i=0,j=0;
char bak[5]="\n is";
char som[50]=" the concatenated array.";
fflush(stdin);

printf("Enter a string:\n");
scanf("%[^\n]s",&string1);
newStrCpy(string1,string2);
printf("The copied string is:\n");
while(string2[i]!='\0')
{
    printf("%c",string2[i]);
    i++;
}
newStrCat(string2,bak);
newStrCat(string2,som);
printf("\nThe conctenated string is:\n");
while(string2[j]!='\0')
{
    printf("%c",string2[j]);
    j++;
}
fflush(stdout);
getch();
}

and the output i got:

Enter a string:
Welcome!!

The copied string is:

Welcome!!
he concatenated string is:
Welcome!!
is the concatenated string
  • 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-06-04T15:17:14+00:00Added an answer on June 4, 2026 at 3:17 pm

    You need to initialize your string2 array:

    char string2[MAX] ={'\0'};
    

    This ensures all members are set to \0[Ref #1] and you don’t have to bother appending a \0 manually to your string array, after copying.
    Currently, your string2 array is never null terminated and hence the while loop continues till it encounters a \0 and it outputs garbage which is not a part of your string.

    Also on a side note,

    fflush(stdin);
    

    makes your program have the beast of Undefined Behavior, do not use it.
    fflush() is guaranteed to work on output stream stdout.


    [Ref #1]
    C99 Standard 6.7.8.21

    If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.

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

Sidebar

Related Questions

I supposed the include-command copy-pastes code in the compilation, it is wrong because the
here is code to copy input string to buffer question is how make such
I am using the following code to copy a folder and it's contents from
In our code base public ActionResult Copy(string id, string targetId) { //lot of similar
This code should copy the string to the generalPasteboard, as the [[UIPasteboard generalPasteboard] string]
I am using the following code to copy text to the clipboard: Clipboard.Open; try
When using the following code to copy a SQLite database from an Assets folder
This code is copy from http://code.google.com/p/closure-library/source/browse/trunk/closure/bin/build/source.py The Source class's __str __method referred self._path Is
I tried to concatenate 2 MP3 files using the code below. I got a
i have written the following code to copy a string hello world to another

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.