Thursday, July 9, 2009

How to concate strings and integers in C++?

Can any one tell me about the concatination function of C++ out of " strcat ".


I want to concate the Integer with string.

How to concate strings and integers in C++?
you need to convert the integer to a char string, then concantenate. you can use itoa() function to do the conversion.


example:





char mystring[] = "testing ";


int myint = 123;


char temp[10];





itoa(myint, temp, 10);


strcat(mystring, temp);





just off the top of my head, but I think its right.
Reply:Something like this:


#include %26lt;sstream%26gt;


...


inline string itos (int n) {stringstream ss; ss%26lt;%26lt;n; return ss.str();}


..





then you can just do things like





string s = "some string";


s+=itos(125);





etc.
Reply:I'm writing the program in C, plz do the changes necessary to make the code in C++. Just use 'cout' instead of 'printf' %26amp; use 'cin' instead of 'scanf'. Okay ...








//equivalent function of "strcat"





#include%26lt;stdio.h%26gt;





main()


{


char s[25],x[25],t[50];





clrscr();


printf("enter string1");


gets(s);


printf("enter string2");


gets(x);


xstrcat(s,x);


getch();


}








xstrcat(char s[25],char x[25])


{


char t[50];


int n,n1,i,j;


n=strlen(s);


n1=strlen(x);


for(i=0;i%26lt;n;i++)


{


t[i]=s[i];


}


for(i=n, j=0; i%26lt;n+n1,j%26lt;n1; i++, j++)


{


t[i]=x[j];


}


t[n+n1]='\0';


puts(t);


}


No comments:

Post a Comment