C++ 字符串去重排序實例代碼
入一個字符串,去掉重復出現的字符,并把剩余的字符串排序輸出。
實現代碼:
#include <iostream> #include <string> using namespace std; void sort(string s) { char tmp[100]; int len=s.size(); int count=0,i,j; for (i=0;i<len;i++) { for (j=i+1;j<len;j++) { if (s[i]==s[j]) { s[j]='0'; } } } for (i=0;i<len;i++) { if (s[i]>='a' && s[i]<='z') { tmp[count++]=s[i]; } } //冒泡排序 for (i=0;i<count;i++) { for (j=0;j<i;j++) { char temp; if (strcmp(&tmp[j],&tmp[i])>0) { temp=tmp[j]; tmp[j]=tmp[i]; tmp[i]=temp; } } } for (i=0;i<count;i++) cout<<tmp[i]; cout<<endl; } void main() { string s; cin>>s; sort(s); } 測試結果,可能想的不周全,歡迎查漏補缺:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答
圖片精選