• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 983
  • Last Modified:

strcat in front

hi,

the function strcat apends the string behind but i want to append the string in front. is there a c function for that ?
thanks
0
zizi21
Asked:
zizi21
  • 2
  • 2
2 Solutions
 
KennethBlokCommented:
StringBuilder sb = new StringBuilder();  
sb.Append("sample");  
sb.Insert(0, "hello-world");  
string s = sb.ToString();
0
 
KennethBlokCommented:
or even easier:

string s= "sample";
s = "hello-world" + s;
0
 
incercCommented:
Hi,

strcat(dest, src) appends the src after the dest and returns dest.

So, to append the string src in front of dest, you have to switch dest with src :
strcat(src, dest)

If you don't want the source being modified, you can use :

strcpy(tmp, src);
strcat(tmp, dest);  //this way, you will have the concatenated string in tmp variable
0
 
Deepu AbrahamR & D Engineering ManagerCommented:
sprtintf() would be a good option:

Have a look at this:

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
char buffer [50];
  char str1[]="Experts-";
  char str2[]="Exchange"
  n=sprintf (buffer, "%d%d",str1,str2);
  printf ("The output is [%s]\n",buffer);

Open in new window

0
 
Deepu AbrahamR & D Engineering ManagerCommented:
My mistake:

Please read this as sprintf (buffer, "%s%s",str1,str2);
char buffer [50];
  char str1[]="Experts-";
  char str2[]="Exchange"
  sprintf (buffer, "%s%s",str1,str2);
  printf ("The output is [%s]\n",buffer);

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now