LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-17-2012, 07:30 AM   #1
Shahid nx
Member
 
Registered: Jan 2012
Posts: 46

Rep: Reputation: Disabled
copy last characters of strings?


Hello Everyone,
Need urgent help. From a string how can i copy last some charterers. like by using this function strncpy (str2,str1,5); will copy first 5 character from str1 to str2 But how can i copy last say 10 character from one string to another. And also sicne i m using array of string so please give me solutuion using sprintf().

Thanks ,
shahid nx

Last edited by Shahid nx; 08-17-2012 at 07:33 AM.
 
Old 08-17-2012, 08:37 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
NOT urgent---We do things when we get to it. If you are on a tight schedule, you might want to hire someone.

I assume you are using C----is there not a function to get the length of a string? Once you know the length of the string, just count back from there.
 
Old 08-17-2012, 09:51 AM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,478

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
There are a bunch of different ways you can skin this cat; possibly the easiest is to simply determine where to start and where to stop.

All strings are arrays, starring at zero and ending at some point with a NULL (\0). So you use the strlen() function to find out how long your incoming string is, subtract 10 from that value then copy character by character to your destination string until you hit the NULL (and be sure to copy the NULL!). Something like this will give the starting point and the length (the stopping point):
Code:
n = strlen (src);       /* how long is src        */
i = n - 10              /* starting point in src  */
j = 0;                  /* starting point in dest */
You need to make sure that i is not less than zero -- even if you believe that your source string is always going to be some length greater than ten you need to cover yourself when it isn't, just in case. So check it
Code:
if (i < 0)
     i = n;
Then all you need to do is
Code:
for ( ; i < n && src [i] != '\0'; i++) {
     dest[j] = src[i];
     j++;
}
dest[j] = '\0';
Here's the whole thing
Code:
#include <stdio.h>
#include <string.h>

void	main	(void)
{
	int	i, j, n;
	char	src [1024] = "this is a long string of stuff to see what happens";
	char	dest [1024];

	n = strlen (src);	/* how long is src		*/
	i = n - 10;		/* starting point in src	*/
	j = 0;			/* starting point in dest	*/
	/*	check length of src				*/
	if (i < 0)
		i = n;
	for ( ; i < n && src [i] != '\0'; i++) {
		dest [j] = src [i];
		j++;
	}
	dest [j] = '\0';
	printf ("%s\n", src);
	printf ("%s\n", dest);
}
And, when executed
Code:
this is a long string of stuff to see what happens
at happens
Hope this helps some.
 
Old 08-18-2012, 01:13 AM   #4
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Rep: Reputation: Disabled
It worked for me, try this :
Code:
/*
str : source string
out : output
n : number of characters
*/
void copyString(char *str,char *out,int n)
{
        strcpy(out,(str+strlen(str)-n));
}
 
Old 09-27-2012, 08:35 PM   #5
KernelJay
LQ Newbie
 
Registered: Aug 2012
Posts: 15

Rep: Reputation: Disabled
That strcpy should really be strncpy to be safe. strncpy allows you to pass the maximum number of bytes which can be copied so as to protect your destination buffer. Unbounded string manipulation can have disastrous results. For some additional insight into why strcpy is so dangerous, check out my latest blog post on the subject:
VERT Vuln School: Stack Buffer Overflows 101

Part 1: Introducing the Bug
Part 2: Explaining the Stack
Part 3: Exploiting the Bug

Thanks,
Craig

Last edited by KernelJay; 09-29-2012 at 09:35 AM. Reason: added part 3 link
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ada: How do I read large blocks of characters and get strings ? muggabug Programming 0 02-10-2012 12:46 PM
Rsync can't copy certain files with unusual characters while cp can smithaa02 Linux - Software 7 11-15-2010 03:26 PM
[SOLVED] Searching and replacing strings in a file with strings in other files xndd Linux - Newbie 16 07-29-2010 03:40 PM
File Copy Issue-Special Characters fortezza Linux - Software 1 11-14-2005 08:16 AM
Copy Paste of data from word to Yahoo/rediff in opera is limited to few characters sachin_keluskar Linux - Software 0 08-19-2004 10:46 AM


All times are GMT -5. The time now is 04:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration