String concatenation in c without using strcat





String concatenation in c without using string functions

#include<stdio.h>

void stringConcat(char[],char[]);
int main(){

    char str1[100],str2[100];
    int compare;

    printf("Enter first string: ");
    scanf("%s",str1);

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

    stringConcat(str1,str2);

    printf("String after concatenation: %s",str1);

    return 0;
}

void stringConcat(char str1[],char str2[]){
    int i=0,j=0;
   
   
    while(str1[i]!='\0'){
         i++;
    }

    while(str2[j]!='\0'){
         str1[i] = str2[j];   
         i++;
         j++;
    }

    str1[i] = '\0';
}

Sample output:
Enter first string: cquestionbank
Enter second string: @blogspot.com
String after concatenation: cquestionbank@blogspot.com






3. Write a c program to delete the all consonants from given string.

5 comments:

Anonymous said...

thank you it is helpful

Unknown said...

My program on String concatenation in c without using strcat is not giving the desired output !
please tell why ?
here is my code
#include
#include
int main()
{
char str1[20], str2[20];
int i=0,ctr,length;
printf("Enter first string->");
scanf("%s",str1);
printf("Enter second string->");
scanf("%s",str2);
while(str1[i]!='\0')
{
ctr=i;
i++;
}
length=ctr+1;
//printf("\nThe lenght of string is->%d",length);
while(str2[i]!='\0')
{
str1[length]=str2[i];
i++;
length++;
}
printf("\nAfter concatenating in str1 ->%s",str1);
return 0;
}

Unknown said...

But when i apply for loop(in second case ) its working! :-)

Unknown said...

I think you need to clear the value of "i" before "while(str2[i]!='\0')"

Emi said...

for(....);
{
for(...)
{
..
...
}
...
}
Explain the execution