我有一个字符串"abc"
。想要从字符串字符中获取所有不同的字符串。
#include<bits/stdc++.h>
using namespace std;
// Function to print all sub strings
void subString(string s, int n)
{
// Pick starting point in outer loop
// and lengths of different strings for
// a given starting point
for (int i = 0; i < n; i++)
for (int len = 1; len <= n - i; len++)
cout << s.substr(i, len) << endl;
}
// Driver program to test above function
int main()
{
string s = "abc";
subString(s,s.length());
return 0;
}
期待:
a b c ab ac bc abc
实际输出:
a b c ab bc abc
答案 0 :(得分:-2)
请参阅以下解决方案:
#include <iostream>
using namespace std;
int main()
{
string input = "abc";
for (int length = 0; length < input.length(); length++) {
for (int offset = 0; offset <= input.length() - length; offset++) {
cout << input.substr(offset, length) + "\n";
}
}
cout << input;
return 0;
}