Please can somebody help me to concatenate names in c.
I try to enter names but anytime it will teat them as separate strings.
this is what I tried to do :
But i was getting segmentation faults,
can you figure anyway out.
Please can somebody help me to concatenate names in c.
I try to enter names but anytime it will teat them as separate strings.
this is what I tried to do :
But i was getting segmentation faults,
can you figure anyway out.
/*
===== ID : 2009FSD16.3M013 =====
===== ASSIGNMENT 007 =====
===== DATE : AUGUST 11, 2009 =====
===== PURPOSE : A function that concatenate 2 words entered from console. =====
*/
//===== START OF CODING =====
#include <stdio.h>
#include <string.h>
//===== PROTOTYPE FOR MYSTRCAT FUNCTION =====
void mystrcat (char a[], char b[]);
//===== start of main code :
main()
{
//declaring character arrays:
char word1 [200];
char word2 [50];
//accepting and reading word from console :
printf("\n\nEnter first word : ");
scanf("%s", word1);
printf("\n\nEnter second word : ");
scanf("%s", word2);
mystrcat (word1, word2);
//printf("\n\nEnter third word : ");
//scanf("%s", word3);
//calling the mystrcat function in the main function :
/* new = strcat (word1, " ");
new1 = strcat (new, word2);
new2 = strcat (new1, " ");
new3 = strcat (new2, word3);
printf("\n\nthe word is %s\n\n", new3);*/
}
//===== START OF CODING THE MYSTRCAT FUNCTION :
void mystrcat (char a [], char b [])
{
//declaring int variables to store the length of the arrays :
int i, l1, l2;
//detecting length of arrays and passing them to a variables :
l1 = strlen (a);
l2 = strlen (b);
//looping through the word2 so it can be add to the first word:
a[l1+i] = 0;
for (i = 0; i <= l2; i++)
{
a[l1+i] = b[i];
}
// sentence to display to console :
printf("\n\nThe concatenated word is %s\n\n", a);
}
//===== END OF CODING THE MYSTRCAT FUNCTION =====
Jump to PostDo you want to actually post the code with regards as to what you want to do? It's blank!
Do you want to actually post the code with regards as to what you want to do? It's blank!
> a[l1+i] = 0;
You didn't initialise i
That means boom today (or boom tomorrow if you're unfortunate)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.