LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 04-07-2006, 06:09 AM   #1
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 183

Rep: Reputation: 30
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
 
Old 04-07-2006, 06:45 AM   #2
Flesym
Member
 
Registered: Aug 2005
Location: Germany
Distribution: Ubuntu, Debian
Posts: 189

Rep: Reputation: 31
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.
Old 04-07-2006, 07:35 AM   #3
baddah
Member
 
Registered: Feb 2006
Location: Cape Town,South Africa
Distribution: Fedora Core 8
Posts: 183

Original Poster
Rep: Reputation: 30
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..
 
Old 10-18-2008, 03:37 PM   #4
charleykadet
Member
 
Registered: Sep 2005
Location: Montreal
Distribution: Fedora Core 5, Gentoo, LFS
Posts: 83

Rep: Reputation: 15
You have to put '\0' at the end of the string...
 
Old 10-19-2008, 12:05 PM   #5
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
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);
}
 
Old 10-19-2008, 12:56 PM   #6
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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>

charsubstring(const charstrsize_t beginsize_t len)
{
  if (
str == || strlen(str) == || strlen(str) < begin || strlen(str) < (begin+len))
    return 
0;

  return 
strndup(str beginlen);
}


int main()
{
  
size_t      begin  5;
  
size_t      end    10;

  const 
charstr    "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  
char*       substr substring(strbeginend);

  
printf("str    = %s\n"str);
  
printf("substr starting from pos %u, and %u characters in length...\n"beginend);
  
printf("substr = %s which has a strlen of %u\n"substrstrlen(substr));

  
free(substr);

  return 
0;

 
Old 02-02-2010, 05:22 AM   #7
aryagaurav001
LQ Newbie
 
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
//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.
 
  


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
replace a substring with another string in C zeppelin Programming 21 11-09-2009 10:59 PM
Replace substring with SED marri Programming 2 07-09-2005 06:18 PM
how to find an exact substring match? ldp Programming 7 02-22-2005 07:28 AM
C++ STL count() number of substring occurences... nyk Programming 4 06-25-2004 08:12 AM
DHCP vendor-encapsulated-options substring bwilliam79 Linux - Networking 1 10-29-2002 11:08 AM


All times are GMT -5. The time now is 09:02 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration