Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie to C: question on strings 1

Status
Not open for further replies.

meBrian

Programmer
Aug 7, 2001
73
US
I am wanting to insert a string into the middle of another string at a particular point (element) and diplay. What is the most efficient way to do this?
 
Well if you want to create a proper string of the result, then you need several calls to strncpy() and strcat() to copy the string fragments in question.

Or if you really just want to print something, then you can cheat by using things like
Code:
printf( "%.5s", string );
to print specific numbers of characters


--
 
Thanks for your response.

I guess because of my ignorance of C I am having trouble just putting together the logic to accomplish it.

I need to do something like this:

string1 = "Hello World"
string2 = "XXX"
Element = 3

Result: "HelXXXlo World"

I'm probably overthinking this so could someone help me out.
Thanks,
 
Code:
#include <stdio.h>
#include <string.h>

int main ( ) {
    char s1[] = &quot;hello world&quot;;
    char s2[] = &quot;!&quot;;
    char result[100];

    strncpy( result, s1, 5 );
    result[5] = '\0';
    strcat( result, s2 );
    strcat( result, &s1[5] );

    printf(&quot;%s\n&quot;, result );
    return 0;
}

--
 
Thanks for the quick reply.

I got it, and it even makes sense!

Thanks.
 
> What is the most efficient way to do this?
That depends on your definition of efficient, i.e. memory requirements, speed of execution, number of lines of code? Of course you also have to consider readability.

Salem's answer is a good solution, and is very readable and easy to follow.

Just for fun, here's another way to accomplish the same task, which is similar to what Salem was suggesting with the printf statement in His first answer.
Code:
#include <stdio.h>
#include <string.h>

int main( ) {

  char s1[] = &quot;hello world&quot;;
  char s2[] = &quot;!&quot;;
  char result[100];

  sprintf(result,&quot;%.5s%s%s&quot;,s1,s2,&s1[5]);

  printf(&quot;%s\n&quot;,result);
  return 0;
}
Of course the magic number 5 can be replaced with a integer variable:
Code:
  int i = 5;
  
  sprintf(result,&quot;%.*s%s%s&quot;,i,s1,s2,&s1[i]);

As far as efficiency, sprintf will be much more complex than strncpy and strcat, so in that sense this soulution may not be any more efficient than Salem's (and is probably less efficient), just fewer lines of code.
 
Or since this is a C++ thread,

string1 = &quot;Hello World&quot;
string2 = &quot;XXX&quot;

string1.insert(string2,3);

 
> Or since this is a C++ thread,

Hmmm...
I wasn't aware there were C++ threads in the C forum.
Good answer though.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top