输出给定字串的全部连续子串。
比如对给给定字符串 “abcd",应该打印出:a,b,c,d,ab,bc,cd,abc,bcd,abcd等。
比如对给给定字符串 “abcd",应该打印出:a,b,c,d,ab,bc,cd,abc,bcd,abcd等。
/*输出给定字串的全部连续子串。
比如对给给定字符串 “abcd",应该打印出:a,b,c,d,ab,bc,cd,abc,bcd,abcd等。*/
/*方法1:使用标准库 string*/
#include <iostream>
#include <string>
using namespace std;
void print(string s)
{
if(s.empty())
return;
string temp;
for(string ::size_type index = 0; index != s.size(); ++ index)
{
temp = "";
for(string :: size_type index1 = index; index1 != s.size(); ++ index1)
{
temp += s[index1];
cout << temp << endl;
}
}
}
int main()
{
string s;
cout << "please input the string:" << endl;
getline(cin, s);
cout << "the print string is:" << endl;
print(s);
system("pause");
return 0;
}
/*方法2:不使用标准库 string*/
#include <iostream>
//#include <string>
#define N 21
using namespace std;
void print(char *s)
{
if(!s)
return;
int len = strlen(s);
for(int index = 0; index < len; ++ index)
{
char temp[N] = "";
for(int index1 = index; index1 < len; ++ index1)
{
temp[index1 - index] = s[index1];
temp[index1 - index +1] = '\0';
cout << temp << endl;
}
}
}
int main()
{
char s[N];
cout << "please input the string:" << endl;
cin.getline(s, N, '\n');
cout << "the print string is:" << endl;
print(s);
system("pause");
return 0;
}
/*方法3:不使用标准库 string*/
#include <iostream>
#define N 21
using namespace std;
void print(char *s)
{
if(!s)
return;
int len = strlen(s);
for(int index = 0; index < len; ++ index)
for(int index1 = index; index1 < len; ++ index1)
{
for(int index2 = index; index2 <= index1; ++index2)
cout << s[index2];
cout << " " << endl;
}
}
int main()
{
char s[N];
cout << "please input the string:" << endl;
cin.getline(s, N, '\n');
cout << "the print string is:" << endl;
print(s);
system("pause");
return 0;
}