1

I'm trying to concatenate two strings together with pointers without using any of the functions.When I enter the two strings for example first hello and then world the output is

hello

world

and not helloworld.Any help would be appreciated.

#include<stdio.h>
#include<stdlib.h>
int main(){
char *s=(char *)malloc(sizeof(char *));
char *s2=(char *)malloc(sizeof(char *));
fgets(s,10,stdin);
fgets(s2,10,stdin);
int i=0;
while(*s){
    s++;
    i++;

}
while(*s2!='\0'){
    *s=*s2;
    s++;
    s2++;
    i++;
}
*s='\0';
printf("%s",s-i);
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
dumb
  • 21
  • 3
  • `fgets` will read the trailing newline character of each input line. You need to remove that if you don't want the concatenated strings to include it. – kaylum Apr 28 '22 at 20:48
  • 2
    The 'wrong' ratio in this code is amply sufficient to strongly suggest you get a well-reviewed *book* on the C language and program development. You *seem* to have a base understanding of *pointers*, which is good. But get a good book; believe me you'll be glad you did. – WhozCraig Apr 28 '22 at 20:57
  • @WhozCraig do u have any suggestions?I'm a beginner and would really like to improve my coding skills. – dumb Apr 28 '22 at 21:36
  • Already mentioned. There are as many *bad* texts on C as there are good ones (arguably more). The basics are covered by most, without too much intellectual damage being done by lower quality ones. A pretty exhaustive list of the more well-known/popular ones [are here](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). What I can absolutely suggest is paddle *away* from "competitive" programming sites at warp speed. They're worthless, and the only thing they "teach" you is how to write really crappy code with really bad habits in short development times. – WhozCraig Apr 28 '22 at 21:43

2 Answers2

2

The program has undefined behavior because you did not allocate memory for entered strings.

char *s=(char *)malloc(sizeof(char *));
char *s2=(char *)malloc(sizeof(char *));
fgets(s,10,stdin);
fgets(s2,10,stdin);

You only allocated memory for two pointers ( sizeof(char *) ).

You need to allocate memory large enough that can contains entered strings and their concatenation in the first character array.

The function fgets can append the new line character '\n' to an entered string. You need to overwrite it.

Also you should not change the original pointers because you need to use them to free the allocated memory.

And take into account that the result string will contain at least 11 characters including the terminating zero character '\0' instead of 10 characters if you are going to enter "hello" and "world" and concatenate them. Though in general it is better to reserve 13 characters if the entered strings will not contain the new line character.

The program can look for example the following way

#include <stdlib.h>
#include <stdio.h>

int main( void )
{
    enum { N = 7 };
    char *s1 = malloc( 2 * N - 1 );
    char *s2 = malloc( N );
    s1[0] = '\0';
    s2[0] = '\0';

    fgets( s1, N, stdin );
    fgets( s2, N, stdin );

    char *p1 = s1;

    while (*p1 != '\n' && *p1 != '\0') ++p1;

    for (char *p2 = s2; *p2 != '\n' && *p2 != '\0'; ++p2)
    {
        *p1++ = *p2;
    }

    *p1 = '\0';

    puts( s1 );

    free( s1 );
    free( s2 );
}

The program output might be

hello
world
helloworld

Instead of these lines

char *s1 = malloc( 2 * N - 1 );
char *s2 = malloc( N );
s1[0] = '\0';
s2[0] = '\0';

you could write

char *s1 = calloc( 2 * N - 1, sizeof( char ) );
char *s2 = calloc( N, sizeof( char ) );

The arrays are zero initialized to keep empty strings in case when calls of fgets will be interrupted.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

fgets() reads to the end of the file or end of line, but includes the end of the line in the data read.

So in your case, you are also concatenating the string with the new lines.

On a different note your statement char *s=(char *)malloc(sizeof(char *)); is allocating memory for sizeof(char*) characters i.e.: the size of a pointer, not X number of characters.

Also since you are concatenating one 10 character string t another, the string needs to be allocated to hold at least that (20 chars + 1 null).

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
lostbard
  • 5,065
  • 1
  • 15
  • 17
  • I suggest that you enclose code in `\`` backticks, otherwise the `*` is interpreted as a formatting character in some cases, and it is not printed. – Andreas Wenzel Apr 28 '22 at 21:18
  • Meanwhile, someone else has fixed the problem for you. My previous comment applies to [revision 2](https://stackoverflow.com/revisions/72050092/2) of your answer, in which several `*` characters are not being printed, because they are being interpreted as formatting characters. – Andreas Wenzel Apr 28 '22 at 23:39