Getting a substring of a char* is a straightforward topic that can be easily accomplished. This can be used for a wide range of programming tasks such as hacking, web crawling, and string manipulation. In this article I will discuss the basic concept and outline the necessary steps for obtaining a substring from a char*.
However, before moving towards the main process, let’s first understand the concept of a substring.
What is a Substring
A substring is only a smaller string of the main text. This smaller “portion” that is being obtained must still contain the original character or characters from the original string. For example, if the original string was “hello world”, then the substring would be “hello” or “world” depending on the desired output.
Get a Substring of a Char
The C users can get a substring of a char through following functions:
1: strncpy() Function
The first way to get a substring of a char* is to use the strncpy() C library function. It copies a specific number of characters from one string to another. To transfer a substring from a char* to a freshly constructed buffer, use this function. Here is an example of how to use strncpy():
Here, pos denotes the beginning index and len is the desired substring’s length.
#include <string.h>
int main()
{
int pos, len;
char str[14] = "abcdefghijklm";
char substring[14];
pos = 6;
len = 5;
printf("Original string is: %s ", str);
printf("\nsubstring is: ");
strncpy(substring,str+(pos-1),len);
printf(substring);
return 0;
}
In this example, substring is the destination buffer, char str is the source string and pos is 6 which means string slicing will start from 6th position (f) and ends at the fifth value, which is j in the case. This will result in a substring buffer of the first 50 characters from the original char* copied from.
Output
2: substr() Function
The second way to get a substring of a char* is to use the substr() C library function. It is used to extract a section of a string based on the starting index and number of characters. This function can be used to return a pointer to the substring or to modify the existing string. An example of substr() usage is the following:
#include <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) != '\0'); i++)
{
*dest = *(src+i);
dest++;
}
*dest = '\0';
return dest-len;
}
int main()
{
char src[] = "We will extract substring from a string";
int m = 6;
int n = 15;
char* dest = substr(src, m, n);
printf("%s", dest);
return 0;
}
In this code, the length and location of the substring’s slice are determined in the user-defined method substr(), and the slice is then placed in the dest variable and printed in the main function. The above code will output the substring in between 6th position (l) and 15 from the start, which is t from the “extract” string. m is the starting point and n is the ending point in this case. Output:
3: memcpy() Function
The third way to get a substring of a char* is to use the memcpy() C library function. It copies several bytes from one memory location to another. To transfer a substring from a char* to a freshly constructed buffer, use this function. An example of memcpy() usage is the following:
#include <string.h>
int main(void)
{
char *text = "This is the full string";
char subtext[7];
memcpy(subtext,&text[9],6);
subtext[6] = '\0';
printf("Original string: %s\n",text);
printf("Substring: %s",subtext);
return 0;
}
In this code, the text variable’s stored string is displayed first, and then the memcpy() method is used to extract the substring, which has the length of 6, the position of 9, and the original text string. The substring is then printed after being saved in the subtext variable.
Output
Conclusion
There are three basic functions to get a substring of a char in C programming language. The functions are strncpy(), substr() and memcpy(). By copying a certain amount of characters from one string to another, you can use the strncpy() function. To extract a portion of string, you can go with the substr() function. While you can use the memcpy() function, which transfers a number of bytes from one memory address to another and thus, can be effective to get a string of a char.