HomeLinuxThe best way to Get a Substring of a char*

The best way to Get a Substring of a char*


Getting a substring of a char* is a simple subject that may be simply completed. This can be utilized for a variety of programming duties similar to hacking, net crawling, and string manipulation. On this article I’ll talk about the essential idea and description the mandatory steps for acquiring a substring from a char*.

Nevertheless, earlier than transferring in the direction of the principle course of, let’s first perceive the idea of a substring.

What’s a Substring

A substring is barely a smaller string of the principle textual content. This smaller “portion” that’s being obtained should nonetheless include the unique character or characters from the unique string. For instance, if the unique string was “good day world”, then the substring could be “good day” or “world” relying on the specified output.

Get a Substring of a Char

The C customers can get a substring of a char by following capabilities:

1: strncpy() Perform

The primary approach to get a substring of a char* is to make use of the strncpy() C library operate. It copies a selected variety of characters from one string to a different. To switch a substring from a char* to a freshly constructed buffer, use this operate. Right here is an instance of the best way to use strncpy():

Right here, pos denotes the start index and len is the specified substring’s size.

#embrace <stdio.h>
#embrace <string.h>

int fundamental()
{
    int pos, len;
    char str[14] = “abcdefghijklm”;
    char substring[14];
    pos = 6;
    len = 5;
    printf(“Authentic string is: %s “, str);
    printf(nsubstring is: “);
    strncpy(substring,str+(pos-1),len);
    printf(substring);
    return 0;
}

 

On this instance, substring is the vacation spot buffer, char str is the supply string and pos is 6 which implies string slicing will begin from sixth place (f) and ends on the fifth worth, which is j within the case. This can end in a substring buffer of the primary 50 characters from the unique char* copied from.

Output

2: substr() Perform

The second approach to get a substring of a char* is to make use of the substr() C library operate. It’s used to extract a piece of a string primarily based on the beginning index and variety of characters. This operate can be utilized to return a pointer to the substring or to change the present string. An instance of substr() utilization is the next:

#embrace <stdio.h>
#embrace <stdlib.h>

char* substr(const char *src, int m, int n)
{
    int len = n-m;
    char *dest = (char*)malloc(sizeof(char)*(len + 1));
    for (int i=m; i<n && (*(src+i) != ‘’); i++)
    {
        *dest = *(src+i);
        dest++;
    }
    *dest = ‘’;
    return dest-len;
}
int fundamental()
{
    char src[] = “We’ll extract substring from a string”;
    int m = 6;
    int n = 15;
    char* dest = substr(src, m, n);
    printf(“%s”, dest);
    return 0;
}

 

On this code, the size and placement of the substring’s slice are decided within the user-defined technique substr(), and the slice is then positioned within the dest variable and printed in the principle operate. The above code will output the substring in between sixth place (l) and 15 from the beginning, which is t from the “extract” string. m is the place to begin and n is the ending level on this case. Output:

3: memcpy() Perform

The third approach to get a substring of a char* is to make use of the memcpy() C library operate. It copies a number of bytes from one reminiscence location to a different. To switch a substring from a char* to a freshly constructed buffer, use this operate. An instance of memcpy() utilization is the next:

#embrace <stdio.h>
#embrace <string.h>

int fundamental(void)
{
    char *textual content = “That is the total string”;
    char subtext[7];
    memcpy(subtext,&textual content[9],6);
    subtext[6] = ‘’;
    printf(“Authentic string: %sn,textual content);
    printf(“Substring: %s”,subtext);
    return 0;
}

 

On this code, the textual content variable’s saved string is displayed first, after which the memcpy() technique is used to extract the substring, which has the size of 6, the place of 9, and the unique textual content string. The substring is then printed after being saved within the subtext variable.

Output

Conclusion

There are three primary capabilities to get a substring of a char in C programming language. The capabilities are strncpy(), substr() and memcpy(). By copying a specific amount of characters from one string to a different, you should utilize the strncpy() operate. To extract a portion of string, you possibly can go together with the substr() operate. Whereas you should utilize the memcpy() operate, which transfers quite a few bytes from one reminiscence tackle to a different and thus, may be efficient to get a string of a char.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments