简体   繁体   English

打印给定字符串的所有子字符串的程序

[英]Program to print all substrings of a given string

I am having trouble creating an algorithm that prints all substrings of a given string.我在创建一个打印给定字符串的所有子字符串的算法时遇到问题。 This is my implementation now:这是我现在的实现:

#include <stdio.h>
#include <string.h>

// Function to print all sub strings 
void subString(char str[], int n)  
{ 
    // Pick starting point 
    for (int len = 1; len <= n; len++)  
    {     
        // Pick ending point 
        for (int i = 0; i <= n - len; i++)  
        { 
            //  Print characters from current 
            // starting point to current ending 
            // point.   
            int j = i + len - 1;             
            for (int k = i; k <= j; k++) {
                char data[n];
                sprintf(data, "%d", str[k]);
                printf("%s\n", data);
            }
        } 
    } 
} 

// Driver program to test above function 
int main()  
{ 
    char str[] = "abc"; 
    subString(str, strlen(str)); 
    return 0; 
} 

My code is not converting integers to strings.我的代码没有将整数转换为字符串。 Could someone help me figure out what's wrong?有人可以帮我弄清楚出了什么问题吗?

The logic seems basically fine, but the formatting doesn't make much sense as this prints the digit values for each character and adds a newline for each print call.逻辑看起来基本没问题,但格式没有多大意义,因为这会打印每个字符的数字值并为每个打印调用添加一个换行符。 If you print the characters directly using %c formatting and only print a newline once you've emitted a full substring you'll have a more sensible result.如果您直接使用%c格式打印字符,并且仅在发出完整子字符串后才打印换行符,则会得到更合理的结果。

#include <stdio.h>
#include <string.h>

void subString(char *str, int n)
{ 
    for (int len = 1; len <= n; len++)
    {     
        for (int i = 0; i <= n - len; i++)  
        { 
            for (int j = i; j <= i + len - 1; j++) 
            {
                putchar(str[j]);
            }

            puts("");
        } 
    } 
} 

int main()  
{ 
    char str[] = "abc";
    subString(str, strlen(str)); 
    return 0; 
} 

Output:输出:

a
b
c
ab
bc
abc

A little nitpick: I'd suggest calling this function printSubStrings since it produces a side effect .一点吹毛求疵:我建议调用这个函数printSubStrings因为它会产生副作用 The name subString doesn't seem to match the contract particularly well.名称subString似乎不太符合合同。

You can also use the "%.*s" format to extract the substring chunk you want instead of the innermost loop:您还可以使用"%.*s"格式来提取您想要的子字符串块,而不是最里面的循环:

void print_substrings(char *str, int n)
{ 
    for (int len = 1; len <= n; len++)
    {     
        for (int i = 0; i <= n - len; i++)  
        {
            printf("%.*s\n", len, str + i);
        }
    } 
} 
#include<bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);    
    string str;
    cin >> str;

    for (int i = 0; i < str.size(); i++) {
        for (int len = 1 ; len <= str.size() - i; len++)
        {
            cout << str.substr(i, len) << endl; // prints substring from starting index i till length len
        }
    }

    return 0;
}

Input:输入:

abcd

Output:输出:

a
ab
abc
abcd
b
bc
bcd
c
cd
d

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

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