#include<iostream>
#include<fstream>
#include <algorithm>
#include<string>
using namespace std;
class Words
{
public:
// 获取输入字符串到文件中
void ppsssWords()
{
ofstream outfile("字符处理.txt");
gets_s(m_strWords);
outfile << m_strWords;
outfile.close();
}
// 获取文件中的单词,输出到数组str中,并进行排序
void paxu()
{
ifstream infile("字符处理.txt");
string str[10], tame;
int count_str = 0;
infile >> str[count_str];
while (!infile.eof())
{
count_str++;
infile >> str[count_str];
}
for (int i = 0; i <= count_str; i++)
{
cout << str[i] << " ";
}
cout << endl;
for (int j = 0; j <= count_str - 1; j++)
{
for (int i = 0; i <= count_str - j - 1; i++)
{
if (str[i][0] > str[i + 1][0])
{
tame = str[i];
str[i] = str[i + 1];
str[i + 1] = tame;//交换相邻的
}
}
}
for (int i = 0; i <= count_str; i++)
{
cout << str[i] << " ";
}
cout << endl;
}
private:
char m_strWords[100];
int t;
};
int main()
{
Words word;
word.ppsssWords();
word.paxu();
}
string数组按照字母排序
最新推荐文章于 2024-05-29 18:19:41 发布