?
Solved

strcat in front

Posted on 2011-02-15
5
Medium Priority
?
947 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:DeepuAbrahamK
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:DeepuAbrahamK
DeepuAbrahamK 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.
Suggested Courses

771 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