Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my code was to copy the string from inputstring to copystring and output was correct
but there were some unwanted signs in the output like inverted ?,", etc

please help me i m new to programming

What I have tried:

C++
#include<stdio.h>
#include<string.h>

int main(){

    char inputstring[100],copystring[100];
    printf("enter the string ");
    gets(inputstring);

    strcat(copystring,inputstring);
    printf("inputstring = %s",inputstring);
    printf("copystring = %s",copystring);


}
Posted
Updated 25-May-18 5:40am
v2

1 solution

strcat concatenates strings. In your code it concatenates a possibly valid string (inputstring) to garbage (copystring is not initialized).
If you just need a copy of the input string, use strncpy instead of strcat.
Please note: never ever use gets. Use fgets with stdin instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900