-1

when in java we can use

sting_name.substring(start, end); 

to get a new substring

    String full_line = "this is a simple line.";
    String part_line;
    System.out.println(full_line);
    part_line = full_line.substring(10, 16);
    System.out.println(part_line);

how can do that same thing in c? is there any function similar to substring?

char full_line[] = "this is a simple line" ;
char part_line[20];
printf("\n%s\n",full_line);
//part_line ;
Deepak
  • 1,503
  • 3
  • 22
  • 45

2 Answers2

0

Use strncpy

Example

#include <stdio.h>
#include <string.h>

int main()
{
   char src[40];
   char dest[12];

   memset(dest, '\0', sizeof(dest));
   strcpy(src, "This is tutorialspoint.com");
   strncpy(dest, src+2, 10);

   printf("Final copied string : %s\n", dest);

   return(0);
}
ProfessionalAmateur
  • 4,447
  • 9
  • 46
  • 63
0

Get a substring of a char*

after looking at above link I tried this: just want to make sure if this is correct way of doing this or there is downside of this?

char full_line[] = "this is a simple line" ;
char part_line[20];
printf("\n%s\n",full_line);
memcpy( part_line, &full_line[10], 6);
part_line[6+1] = '\0';
printf("\n%s\n",part_line); 
Community
  • 1
  • 1
Deepak
  • 1,503
  • 3
  • 22
  • 45