简体   繁体   English

查找字符串的所有子字符串

[英]Find all substrings of a string

I have a string where I need to do substring operation.我有一个字符串,我需要在其中进行子字符串操作。 I'm trying to achieve something like this for example if the input string is com then the output must be something like this -我正在尝试实现这样的东西,例如如果输入字符串是com那么输出必须是这样的 -

c co com o om m .. I have tried this c co com o om m .. 我试过了

for(int i=0 ; i<len ;i++)
{
    printf("%s",&string[strlen(string)-i]));
}

A substring is defined by its left and right ends so there are O(n*n) substrings in a string of length n .子串由其左右两端定义,因此长度为n的字符串中有O(n*n)个子串。

int n = strlen(string);
for(int i = 0; i < n; i++)
{   for(int j = i; j < n; j++)
    {   /* print substring from i to j */
        for(int k = i; k <= j; k++)
        {   printf("%c", string[k]);
        }
        printf("\n");
    }
}

You're missing a comma in your code:您的代码中缺少逗号:

for(int i=0 ; i<len ;i++)
{
    printf("%s", &string[strlen(string)-i])
}

But that will print "", "m", "om" - not what you want.但这会打印 "", "m", "om" - 不是你想要的。

Something more like:更像是:

// start at each point in the string
for ( const char *start = string; *start; ++start )
{
  // for each starting point, go from the whole remainder down
  // to just one character
  for ( const char *end = string + strlen(string); end > start; --end )
  {
    for ( const char *s = start; s < end; ++s )
      putchar(*s);

    putchar('\n');   
  }
}

Example: https://ideone.com/XXoYv6示例: https : //ideone.com/XXoYv6

  • Substring means any contiguous group of characters.子字符串表示任何连续的字符组。
  • For the n string it will generate (n*(n-1) /2) substrings.对于 n 个字符串,它将生成 (n*(n-1) /2) 个子字符串。
  • For example of String source = "STACK" Length of the character is 5, so it total substring would be (5(5-1) /2) = 10例如 String source = "STACK" 字符的长度为 5,因此总子串将为 (5(5-1) /2) = 10

We have to iterate through the first element of string and print all the substring and than onwards one by one we increment the index of i and printing the substring from range (j to k)我们必须遍历字符串的第一个元素并打印所有子字符串,然后我们逐一增加 i 的索引并打印范围 (j 到 k) 的子字符串

public void generateSubString(String source){
        char[] arr = source.toCharArray();
        for(int i = 0; i<arr.length; i++){
            for(int j = i ; j < arr.length; j++){
                for(int k = i; k<=j; k++){
                    System.out.print(arr[k]);
                }
                System.out.println();
            }

OUTPUT:输出:

S ST STA STAC STACK T TA TAC TACK A AC ACK C CK K S ST STA STAC 堆栈 T TA TAC TACK A AC ACK C CK K

Below is the code to find all substrings of a string in javascript without for loop, which will increase the speed of code.下面是在没有for循环的情况下在javascript中查找字符串的所有子字符串的代码,这将提高代码速度。

const devideSubStr = (str) => {
 var totalLoop = str.length * ((str.length + 1)/2);
 // looping count
 let i = 0;
 var totalChar = 1;//character to get
 var charFrom = 0;// from which index
 var strLength = str.length;//length of digit
 while( i < totalLoop){     
      console.log(str.substr(charFrom, totalChar))
      charFrom ++;
      i ++;
      if(charFrom == strLength){
          charFrom = 0;
          strLength = strLength - 1;
          totalChar ++;
      }  
 }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM