0

I have written this code from an algorithm given by my friend. But when I try to implement it it's not working. Can anyone tell me error here?

#include<stdio.h>
void scat(char [], char []);
int i,j;
void main()
{

    char s1[10],s2[10];
    printf("Enter first string: ");
    scanf("%s",&s1);

    printf("Enter second string: ");
    scanf("%s",&s2);

    scat(s1,s2);
}

void scat(char s1[], char s2[])
{
    char str1[10],str2[10],str3[20];

    for(i=0;str1[i]!=NULL;i++)
        str3[i]=str1[i];

    for(j=0;str2[j]!=NULL;j++,i++)
        str1[i]=str3[j];

    printf("\nConcanated string is %s",str3);
}
Steve Friedl
  • 3,929
  • 1
  • 23
  • 30

3 Answers3

1
#include<stdio.h>
void scat(char [], char[]);
int main()
{

    char s1[10], s2[10];
    printf("Enter first string: ");
    scanf("%s", s1);

    printf("Enter second string: ");
    scanf("%s", s2);

    scat(s1, s2);
    return 0;
}

void scat(char s1[], char s2[])
{
    char str3[20];
    int i,j;
    for (i = 0; s1[i] != '\0'; i++)
        str3[i] = s1[i];

    for (j = 0; s2[j] != '\0'; j++, i++)
        str3[i] = s2[j];
    str3[i] = '\0';
    printf("\nConcanated string is %s", str3);
}

U used undeclared variables in function. Also I fix some mistakes you made.

Maqcel
  • 493
  • 5
  • 16
  • Thanks brother @MerMonkey for help. I wrote same code as yours after fixing it myself. –  Nov 03 '19 at 17:21
1

What are you copying? str1 into str3 and str3 into s1. What does it mean ?. Even str1, str2 have nothing useful in them. Just unknown character so copying them is undefined. Try this.

void scat(char *s1, char *s2)
{
    char str3[20];

    for(i=0;s1[i]!='\0';i++){
        str3[i]=s1[i];}

    for(j=0;s2[j]!='\0';j++,i++)
        str3[i]=s2[j];

    str3[i]= '\0'; // This is must

    printf("\nConcatenated string is %s",str3);
}

Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20
  • Thanks I already found the solution but i learnt something new from that `*s1` and `*s2`. I thing it will point to the first element of array, Right or not?? –  Nov 03 '19 at 17:20
  • You must know this thing. Most of the time when you are dealing with an array in actual they are pointers to the first element of an array. Like when you pass an array to any function. The compiler automatically convert it to pointer to first element of the array. This is implicit casting by compiler. – Nikhil Badyal Nov 03 '19 at 17:23
  • Yeah brother I wasn't know that method to implement in function. I just write like this `void scat(char s1[], char s2[])` –  Nov 03 '19 at 17:26
  • It's ok to write like this. The compiler won't issue any warning, but you should know how things are happening behind the scenes. Even in my code, if you edit and write `void scat(char s1[], char s2[])` it goona work. But for making you understand this concept i wrote it intentionally. – Nikhil Badyal Nov 03 '19 at 17:31
0

for c, strings are simply a pointer to char arrays, so they can't really be concatenated. but we can combine them in another way. we can use strcat from the string.h library. this works:

#include <stdio.h>
#include <string.h>
#define MAX_LIMIT 200

int main() {    char str1[200], str2[200];

    printf("Enter the first string: ");     
    fgets(str1, MAX_LIMIT, stdin);

    printf("Enter the second string: ");    
    fgets(str2, MAX_LIMIT, stdin);

    strcat(str1, str2);

    printf("\nConcanated string is %s", str1);

    return 0; }
  • "*i also use the no warnings because of the scanf dangers*" but there is no appearance of `scanf() in the code you show. – alk Nov 03 '19 at 11:21
  • Also `fgets()` is *not* a 1:1 replacement for `fscanf("%s", ...)`. – alk Nov 03 '19 at 11:23
  • `fgets(str1, MAX_LIMIT, stdin);` what does this wants to say?? –  Nov 03 '19 at 17:23