C substring program lớn 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" & "e." The header file "string.h" does not contain any library function khổng lồ 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 "); gets(string);  printf("Enter the position and length of substring "); scanf("%d%d", &position, &length);  while (c length) sub = string; c++; sub = "";  printf("Required substring is "%s" ", sub); // """ to lớn 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, & length of the required substring. As we use call by reference, we bởi not need to lớn return the substring array. See another code below in which we return a pointer to lớn 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 "); gets(string);  printf("Enter the position và length of substring "); scanf("%d%d", &position, &length);  substring(string, sub, position, length);  printf("Required substring is "%s" ", 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 khổng lồ string. String address, required length of substring & position from where to lớn extract substring are the three arguments passed lớn 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 "); gets(string);  printf("Enter the position and length of substring "); scanf("%d%d", &position, &length);  p = substring(string, position, length);  printf("Required substring is "%s" ", p);  free(p);  return 0;

/*C substring function: It returns a pointer khổng lồ 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. "); 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<100>, *p; int position = 1, length = 1, t, string_length;  printf("Enter a string "); gets(string);  t = string_length = strlen(string);  printf("Substring of "%s" are ", string);  while (position string_length) while (length t) p = substring(string, position, length); printf("%s ", p); free(p); length++; t--; position++; length = 1;   return 0;

/* Use substring function given in above sầu 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 & divideCheông xã vowelRoots of quadratic equationLeap year program in CSum of digitsFactorial program in CHCF & LCMDecimal khổng lồ 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 tìm kiếm 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 *