?
Solved

How can I copy the string from a specified position in C?

Posted on 2003-03-11
6
Medium Priority
?
346 Views
Last Modified: 2008-03-06
In order to rotate the 2 last charaters in a word, I use the following way
For example, s[] = "Hello";
I would like to copy "lo" to another array with strcpy and then use strcat to concatenate the string "Hel" after "lo" to form "loHel".But I can only copy the whole string to another array, I don't know how to specify an exact position. Thanks, I'm just newbie in C.
0
Comment
Question by:xiuxiull
[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
6 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8112798
use memcpy...

char orig[] = "Hello"
char newStr[6];

memcpy(newStr, orig[strlen(orig)-2], 3);/* 3 to copy the null*/
memcpy(newStr[2], orig, strlen(orig)-2);
0
 
LVL 3

Accepted Solution

by:
Nosfedra earned 152 total points
ID: 8112813
since s is actually a pointer, you can use pointer arithmetics to position yourself wherever you want in the string.
Take a look also at strncpy, strncat.

You should do something like:
char buf[20];
strcpy(buf,s+3);
strncat(buf,s,3);
buf[strlen(buf)] = 0; // these are all null-terminated strings so you should end the string with 0.

You have to get familiar with string manipulation routines.

hth...
0
 
LVL 6

Assisted Solution

by:gj62
gj62 earned 148 total points
ID: 8112820
Whoops, try this:

char orig[] = "Hello";
char newStr[6];

memcpy(newStr, &orig[strlen(orig)-2], 2);
memcpy(&newStr[2], orig, strlen(orig)-2);
newStr[strlen(orig)]=0;
0
 

Expert Comment

by:CleanupPing
ID: 9474554
xiuxiull:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

764 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