Hi,
This question may be silly but I want to know about any fucntion that concatenates 2 strings without changing any of them!
strcat changes the destination string.
I need something like dest = src1+src2.
Thanks,
Angkar
Hi,
This question may be silly but I want to know about any fucntion that concatenates 2 strings without changing any of them!
strcat changes the destination string.
I need something like dest = src1+src2.
Thanks,
Angkar
strcat does not change the string, per se. The first character of str2 overwrites the null character in str1, and appends the rest of the string with a null character at the end.
There's not really a way around it. Other C functions and stuff depend on the null character to flag the end of the string. In the case of strings, that's all null is there for.
Last edited by whiteflags; 04-23-2006 at 05:20 PM.
strcat It takes two arguments. The first is the one to be copied to. The second one is what you want added onto the end. SoAdds "test' to the end of string. it returns a pointer to the string with the new ending.Code:strcat(string,"test");
~Sven
Last edited by 00Sven; 04-23-2006 at 05:24 PM.
Windows XP Home Edition - Dev-C++ 4.9.9.2
board.theprogrammingsite.comOriginally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
So, any suggestion how to keep both the strings as they are and generate a new string that is S1 + S2?
I think you are trying to concatenate a string that is too big. The first array has to be big enough to fit both of the strings:
Code:char bigstr[80] = strcat( "Pretend I am a really, really long", " run-on sentence." ); puts(bigstr);
Last edited by whiteflags; 04-23-2006 at 05:43 PM.
Nope I'm using a string in loop...say "Hi"
So I'm getting :
HiMatt
HiMattChris
HiMattChrisKellie
.
.
.
as o/p instead of
HiMatt
HiChris
HiKellie
.
.
.
Note: sizeof should be a constant expression, but still my compiler doesn't like it so change if necessary.Code:char src1[] = ""; char src2[] = ""; char dest[sizeof(src1) + sizeof(src2) - 1]; strcpy(dest, src1); strcat(dest, src2);
The only way to do this would be to copy the string that you want something to be added to and then use strcat.~SvenCode:char str1[10] = "Original"; char str2[10] = "with new ending"; char final[20]; char temp[10]; strcpy(temp,str1); final = strcat(temp, str2);
Windows XP Home Edition - Dev-C++ 4.9.9.2
board.theprogrammingsite.comOriginally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
Since final becomes a constant char pointer here this will cause an error.Originally Posted by 00Sven
You can also just use strcat(temp,str2) wherever you would use the variable.
~Sven
Windows XP Home Edition - Dev-C++ 4.9.9.2
board.theprogrammingsite.comOriginally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
There is no need for a temporary string, strcat(dest, src) appends src to the end of dest, which it sees as the null terminator, which is 0. So if your dest string is "hi" just set dest[2] = 0.
For example:
Code:int i; char hi[32] = "hi"; char *hiList[] = {" bob", " janet", " jim"}; for (i = 0; i < 3; i++) { hi[2] = 0; printf("%s\n", strcat(hi, hiList[i])); }
Perhaps a sprintf?
Code:{ sprintf( new_string, "%s%s", str1, str2 ); }
What i have learned after looking at this long discussion is that AngKar needs a function that can concatenate two strings in third string ? That means the original strings should not be changed.
OK try the following code :
Now you can change the ARRAY size as desired by you.Code:#include<stdio.h> #include<conio.h> int main() { char a[20],b[20],result[40]; int i=0,j=0; clrscr(); printf("\n Enter first string less than 20 chars\t"); gets(a); printf("\n Enter second string less than 20 chars \t"); gets(b); for(i=0;a[i]!='\0';i++) { result[i]=a[i]; } result[i]=' '; i=i+1; for(j=0;b[j]!='\0';j++,i++) { result[i]=b[j]; } result[i]='\0'; printf("\n Final string is \t%s",result); getch(); return 0; } OUTPUT : Enter first string less than 20 chars Hi i am NAPSTER Enter second string less than 20 chars I am a new member FINAL STRING IS Hi i am NAPSTER I am a new member
REALNAPSTER
OK a few things are wrong with that. First of all you should never use gets. ReplacewithCode:gets(a);fgets takes 3 arguments. First is the same as the argument of gets. Second is the number of characters or the size of input to get. The last one is the stream to read from, in this case it is stdin. fgets will not overflow an array like gets will.Code:fgets(a,sizeof(a),stdin);
Next thing to fix is that you do not need to do all of the for loops and stuff. Just change that all to-sprintf is like printf but it prints it to a string in the first argument.Code:int main(void){ printf("Enter a string:"); fgets(a,sizeof(a),stdin); printf("Enter another string:"); fgets(b,sizeof(b),stdin); sprintf(result,"%s%s",a,b); printf("Final string is\t%s",result); getchar(); return 0; }
~Sven
Last edited by 00Sven; 04-24-2006 at 12:55 PM.
Windows XP Home Edition - Dev-C++ 4.9.9.2
board.theprogrammingsite.comOriginally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
Thanks 00seven for telling this new concept to me. IT's really cool to learn something from a agemate.
REALNAPSTER