Solved

strcat in front

Posted on 2011-02-15
5
939 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
  • 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 100 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 400 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

776 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