How can I concatenate a given string with a string I take as input? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How can I concatenate a given string with a string I take as input?

7th Jul 2018, 7:30 AM
Syed Naqvi
Syed Naqvi - avatar
8 Answers
+ 2
you can use strcat function. #include<string.h> main() { char s1[100] , char s2[] = "string 2"; scanf ("%s", s1); strcat(s1,s2); } in s1 string in we have to concatenate, s2 which string is to concatenate. final concatenated string will be s1. [EDIT]: or if you want to implement it from scratch, you can do it using loops.
7th Jul 2018, 7:52 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 8
you can use string function in c , like : strcat() function is used to join two strings, https://www.programiz.com/c-programming/library-function/string.h/strcat
7th Jul 2018, 12:34 PM
Lakhvir Singh
Lakhvir Singh - avatar
+ 1
c
7th Jul 2018, 7:47 AM
Syed Naqvi
Syed Naqvi - avatar
+ 1
plz tell me using + operator
7th Jul 2018, 8:06 AM
Syed Naqvi
Syed Naqvi - avatar
+ 1
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int i = 4; double d = 4.0; char s[] = "HackerRank "; // Declare second integer, double, and String variables. int a,j=0,k=0; double b; char c[105],s3[105]; // Read and save an integer, double, and String to your variables. scanf("%d\n",&a); scanf("%lf\n",&b); scanf("%s",c); //scanf("%*[^\n]%*c", &c); // Print the sum of both integer variables on a new line. printf("%d\n",i+a); // Print the sum of the double variables on a new line. printf("%.1f\n",d+b); // Concatenate and print the String variables on a new line // The 's' variable above should be printed first. printf("%s",s); printf("%s",c); return 0; }
7th Jul 2018, 1:24 PM
Syed Naqvi
Syed Naqvi - avatar
+ 1
You must enter an integer a double and a string. It will add the numbers and concatenate the strings. but in this case only first word of the string concatenates the rest don't. please correct it
7th Jul 2018, 1:27 PM
Syed Naqvi
Syed Naqvi - avatar
0
different language has different ways to do. specify for which language you are asking.
7th Jul 2018, 7:45 AM
Nikhil Dhama
Nikhil Dhama - avatar
0
Naqvi Viyank c doesn't follow the oops concepts, so you can't overload the + operator in c, but yes you can do it in cpp. in python and some other languages, it's already handy given to you.
7th Jul 2018, 8:13 AM
Nikhil Dhama
Nikhil Dhama - avatar