Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
04-07-2006, 06:09 AM
#1
Member
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 178
Rep:
extract substring from string in C
hallo,is there no function in C (like substr in PHP) to extract a substring from one string into another.for instance if I have
char string1[10];
if string1 = '12345678' how can i store '34567' into another string(in PHP substring(string1,3,4) would have worked.)
thanks
04-07-2006, 06:45 AM
#2
Member
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189
Rep:
Of course you can do this in C. Have a look at the following link where you will find all standard C string functions:
http://www.cppreference.com/stdstring/index.html
For your purpose you should use the function 'strncpy'
Here an example:
Code:
#include <string.h>
#include <stdlib.h>
main(){
const char* from = "12345678";
char *to = (char*) malloc(6);
strncpy(to, from+2, 5);
}
1 members found this post helpful.
04-07-2006, 07:35 AM
#3
Member
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 178
Original Poster
Rep:
thanks,that worked!although i must confess i'm a bit rusty on the whole C pointer thing.I'll just use it for now,figure out the details later..
10-18-2008, 03:37 PM
#4
Member
Registered: Sep 2005
Location: Montreal
Distribution: Fedora Core 5, Gentoo, LFS
Posts: 83
Rep:
You have to put '\0' at the end of the string...
10-19-2008, 12:05 PM
#5
Senior Member
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Quote:
You have to put '\0' at the end of the string...
Yeah - but using strndup you don't have to worry about null termination as it adds it and allocating memory as it does the malloc for you so ..
Code:
#include <string.h>
main(){
const char* from = "12345678";
char *to;
to=strndup(from+2, 5);
}
10-19-2008, 12:56 PM
#6
Senior Member
Registered: Jun 2006
Location: Maryland
Distribution: Ubuntu
Posts: 1,070
You also have to free any allocated memory, including if you use strdup().
Here's another take on what otherwise should have been a simple exercise:
PHP Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char * substring (const char * str , size_t begin , size_t len )
{
if ( str == 0 || strlen ( str ) == 0 || strlen ( str ) < begin || strlen ( str ) < ( begin + len ))
return 0 ;
return strndup ( str + begin , len );
}
int main ()
{
size_t begin = 5 ;
size_t end = 10 ;
const char * str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
char * substr = substring ( str , begin , end );
printf ( "str = %s\n" , str );
printf ( "substr starting from pos %u, and %u characters in length...\n" , begin , end );
printf ( "substr = %s which has a strlen of %u\n" , substr , strlen ( substr ));
free ( substr );
return 0 ;
}
02-02-2010, 05:22 AM
#7
LQ Newbie
Registered: Feb 2010
Posts: 1
Rep:
//simple program to extract substring from a given string
#include<stdio.h>
#include<conio.h>
#include<string.h>
void extract(char *,char *,char *,int,int);
void main()
{
char s[50]="working with strings is fun";
char t[50];
char d[50];
int pos,len;
printf("enter the position and length to be extracted= ");
scanf("%d%d",&pos,&len);
printf("\n\n");
extract(s,t,d,pos,len);
puts(d);
getch();
}
void extract(char *s,char *t,char *d,int pos,int len)
{
s=s+(pos-1);
t=s+len;
while(s!=t)
{
*d=*s;
s++;
d++;
}
*d='\0';
}
email id:-aryagaurav_dce_cs@yahoo.co.in
Last edited by aryagaurav001; 02-02-2010 at 06:29 AM .
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 08:01 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News