Solved

strcat in front

Posted on 2011-02-15
5
943 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 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
difference between mmap and malloc/valloc + mmap fixed 18 277
Test against App 49 138
Using ANSI C how to Read a .csv file 10 115
Please explain C language error codes ? 5 45
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

726 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