LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   extract substring from string in C (https://www.linuxquestions.org/questions/programming-9/extract-substring-from-string-in-c-432620/)

baddah 04-07-2006 05:09 AM

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

Flesym 04-07-2006 05:45 AM

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);
}


baddah 04-07-2006 06:35 AM

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..

charleykadet 10-18-2008 02:37 PM

You have to put '\0' at the end of the string...

bgeddy 10-19-2008 11:05 AM

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);
}


dwhitney67 10-19-2008 11:56 AM

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;



aryagaurav001 02-02-2010 04:22 AM

//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


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