HomeTHỦ THUẬT - PHẦN MỀMSubstring c# là gì

Substring c# là gì

17:38, 17/03/2021

C substring program khổng lồ find substring of a string & 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 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 print "  return 0;

C substring program output:

*

Substring in C language using function

We create a function & pass it four arguments original string array, substring array, position, và length of the required substring. As we use Điện thoại tư vấn by reference, we vày 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 to string. String address, required length of substring and position from where lớn extract substring are the three arguments passed khổng lồ function.

Xem thêm: V-Sync Là Gì ?” Cách Lựa Chọn Về Việc Bật Hay Tắt Vsync? Trả Lời “Vsync 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 & 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 to the substring */

char *substring(char *string, int position, int length) char *p; int c;  p = malloc(length+1); if (p == NULL) printf("Unable khổng lồ 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 chất lượng, if all the characters in the string are different.

Xem thêm: Tổng Hợp Đáp Án Câu Trả Lời Nào Đúng Khi Nói Về Dao Găm Sao Băng Của Sát Thủ?

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 divideCheông xã vowelRoots of quadratic equationLeap year program in CSum of digitsFactorial program in CHCF and LCMDecimal lớn binary in CnCr & nPrAdd n numbersSwapping of two numbersReverse a numberPalindrome numberPrint PatternDiamondPrime numbersArmstrong numberArmsvào 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 sầu 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 sầu Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.


Chuyên mục: THỦ THUẬT - PHẦN MỀM