Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

strcat in front

Posted on 2011-02-15
5
Medium Priority
?
952 Views
Last Modified: 2012-05-11
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
Comment
Question by:zizi21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 5

Expert Comment

by:KennethBlok
ID: 34895204
StringBuilder sb = new StringBuilder();  
sb.Append("sample");  
sb.Insert(0, "hello-world");  
string s = sb.ToString();
0
 
LVL 5

Expert Comment

by:KennethBlok
ID: 34895209
or even easier:

string s= "sample";
s = "hello-world" + s;
0
 
LVL 4

Accepted Solution

by:
incerc earned 400 total points
ID: 34895246
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
 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 34896374
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
 
LVL 11

Assisted Solution

by:Deepu Abraham
Deepu Abraham earned 1600 total points
ID: 34896478
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question