Or you want a quick look: Substring in C language using function
C substring program to find substring of a string and its all substrings. A substring is itself a string that is part of a longer string. For example, substrings of string "the" are "" (empty string), "t", "th", "the", "h", "he" and "e." The header file "string.h" does not contain any library function to find a substring directly.
Bạn đang xem: Substring c# là gì
#include int main() { char string, sub; int position, length, c = 0; printf("Input a stringn"); gets(string); printf("Enter the position and length of substringn"); scanf("%d%d", &position, &length); while (c length) { sub = string; c++; } sub = " "; printf("Required substring is "%s"n", sub); // """ to print " return 0;}
C substring program output:
Substring in C language using function
We create a function and pass it four arguments original string array, substring array, position, and length of the required substring. As we use call by reference, we do not need to return the substring array. See another code below in which we return a pointer to substring, which we create in our function using dynamic memory allocation.
#include void substring(char , char, int, int); int main() { char string, sub; int position, length, c = 0; printf("Input a stringn"); gets(string); printf("Enter the position and length of substringn"); scanf("%d%d", &position, &length); substring(string, sub, position, length); printf("Required substring is "%s"n", sub); return 0;}// C substring function definitionvoid substring(char s, char sub, int p, int l) { int c = 0; while (c l) { sub = s
; c++; } sub = " ";}
C substring program using pointers
To find substring we create a substring function which returns a pointer to string. String address, required length of substring and position from where to extract substring are the three arguments passed to function.
Xem thêm: " Người Tham Dự Tiếng Anh Là Gì ? Phân Biệt Attendee Và Attendant Trong Tiếng Anh
#include #include
char* substring(char*, int, int);
int main(){ char string, *p; int position, length; printf("Input a stringn"); gets(string); printf("Enter the position and length of substringn"); scanf("%d%d", &position, &length); p = substring(string, position, length); printf("Required substring is "%s"n", p); free(p); return 0;}
/*C substring function: It returns a pointer to the substring */
char *substring(char *string, int position, int length){ char *p; int c; p = malloc(length+1); if (p == NULL) { printf("Unable to allocate memory.n"); exit(1); } for (c = 0; c length; c++) { *(p+c) = *(string+position-1); string++; } *(p+c) = " "; return p;}
#include #include #include char* substring(char*, int, int); int main() { char string, *p; int position = 1, length = 1, t, string_length; printf("Enter a stringn"); gets(string); t = string_length = strlen(string); printf("Substring of "%s" aren", string); while (position string_length) { while (length t) { p = substring(string, position, length); printf("%sn", p); free(p); length++; } t--; position++; length = 1; } return 0;}
/* Use substring function given in above C program*/
Substring program output:
Number of substrings of a string
A string of length n has substrings. They are unique, if all the characters in the string are different.
Consider the string: a1a2a3..............an-1an
Number of substrings of length n: 1
Number of substrings of length (n-1): 2
Number of substrings of length (n-2): 3 ..................Number of substrings of length 3: (n-3)
Number of substrings of length 2: (n-2)
Number of substrings of length 1: (n)
Number of substrings of length 0 (empty string): 1
C Hello worldPrint IntegerAddition of two numbersEven oddAdd, subtract, multiply and divideCheck vowelRoots of quadratic equationLeap year program in CSum of digitsFactorial program in CHCF and LCMDecimal to binary in CnCr and nPrAdd n numbersSwapping of two numbersReverse a numberPalindrome numberPrint PatternDiamondPrime numbersArmstrong numberArmstrong numbersFibonacci series in CFloyd"s triangle in CPascal triangle in CAddition using pointersMaximum element in arrayMinimum element in arrayLinear search in CBinary search in CReverse arrayInsert element in arrayDelete element from arrayMerge arraysBubble sort in CInsertion sort in CSelection sort in CAdd matricesSubtract matricesTranspose matrixMatrix multiplication in CPrint stringString lengthCompare stringsCopy stringConcatenate stringsReverse string Palindrome in CDelete vowelsC substringSubsequenceSort a stringRemove spacesChange caseSwap stringsCharacter"s frequencyAnagramsC read fileCopy filesMerge two filesList files in a directoryDelete fileRandom numbersAdd complex numbersPrint dateGet IP addressShutdown computer
Programming Simplified is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.