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<1000>, sub<1000>; int position, length, c = 0;  printf("Input a string\n"); gets(string);  printf("Enter the position and length of substring\n"); scanf("%d%d", &position, &length);  while (c length) { sub = string; c++; } sub = "\0";  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<1000>, sub<1000>; int position, length, c = 0;  printf("Input a string\n"); gets(string);  printf("Enter the position and length of substring\n"); 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 = "\0";}

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: 10 - Khám Phá Ngay Tài Liệu Srs Là Gì


#include #include

char* substring(char*, int, int);

int main(){ char string<100>, *p; int position, length;  printf("Input a string\n"); gets(string);  printf("Enter the position and length of substring\n"); 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) = "\0";  return p;}
#include #include #include  char* substring(char*, int, int); int main() { char string<100>, *p; int position = 1, length = 1, t, string_length;  printf("Enter a string\n"); gets(string);  t = string_length = strlen(string);  printf("Substring of \"%s\" are\n", string);  while (position string_length) { while (length t) { p = substring(string, position, length); printf("%s\n", 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.

Xem thêm: Sale Promotion Là Gì - Sales Promotion Là Gì

Consider the string: a1a2a3..............an-1an

Number of substrings of length n: 1 <(a1a2a3..............an-1an)>

Number of substrings of length (n-1): 2 <(a1a2a3..............an-1), (a2a3..............an-1an)>

Number of substrings of length (n-2): 3 <(a1a2a3..............an-2), (a2a3..............an-1), (a3a3..............an)>..................Number of substrings of length 3: (n-3) <(a1a2a3), (a2a3a4), (a3a4a5),................,(an-2an-1an)>

Number of substrings of length 2: (n-2) <(a1a2), (a2a3), (a3a4),...........,(an-1an)>

Number of substrings of length 1: (n) <(a1), (a2), (a3),...........,(an-1), (an)>

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.

Bài viết liên quan

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *