❶ (c語言)輸入一串字元,按從小到大順序排列並刪除重復項。
#include<stdio.h>
#include"string.h"
int main(void)
{
char str1[500]={0},str2[256]={0};//定義二個數組,並賦初值為0
int i;
gets(str1);//讀取一個字元串
for(i=0;str1[i];i++)
{
str2[str1[i]]=1;//str1中每個字元的assic碼作為str2的下標值,並把對應位置填充為1,同一個字元的assci值相同,所以這樣就去掉了重復字元
}
for(i=0;i!=256;i++)
if(str2[i]==1)//判斷數組中被str1填充的位置,填充的是非0值,沒填充的是0值
printf("%c",i);//輸出str2的下標值,對應str1中的字元值
putchar('\n');
return 0;
}
❷ #c語言編程#實現將字元串中所有重復的字元剔除,將剔除之後的輸出 如sajsk輸出ajk
#include <stdio.h>
#include <string.h>
void sort(char s[]) { // 選擇排序
int i,j,k,t,len = strlen(s);
for(i = 0; i < len - 1; ++i) {
k = i;
for(j = i + 1; j < len; ++j) {
if(s[k] < s[j]) k =j;
}
if(i != k) {
t = s[i];
s[i] = s[k];
s[k] = t;
}
}
}
// 將不重復的字元復制到t[],返回刪除的字元個數
int change(char s[], char t[]) {
int i = 1,j = 0,cnt = 0;
sort(s);
t[0] = s[0];
while(s[i]) {
if(s[i] == s[i - 1]) ++cnt;
else t[++j] = s[i];
++i;
}
t[j] = '\0';
return cnt;
}
int main() {
char s[] = "aseqkwh wkqhasweewwqbkh112504ffvsdr";
char t[60];
printf("原串:%s\n",s);
printf("共刪除%d個字元。\n",change(s,t));
printf("最後得到的字元串為:%s\n",t);
return 0;
}
❸ C語言去掉重復字元再排序
char a[30] = "qweasdzxcvbnhbgfvredcxswqazx";
for (int i = 0; i < 30; i++) {
printf("%c",a[i]);
for (int j = 0; j < i; j++) {
if (a[i] == a[j]) {
a[i] = '0';
}
}
}printf("\n");
for (int i = 0; i< 30; i++) {
if (a[i] != '0') {
printf("%c",a[i]);
}
}
用兩個for循環,將字元串的每一個字元和前一個進行比較,如果有一個相同,將他等於字元串內不同的一個字元(很重要),然後另作一個for循環,將不含有這個字元的字元串輸出
qweasdzxcvbnhbgfvredcxswqazx
qweasdzxcvbnhgfr
還給你一個方法,先排序,然後將,a[i]==a[i+1]的時候,將a[i]='0',也可以做出來,但是位置變化了
❹ C語言中刪除字元串中的重復字元,然後輸出新的字元串
#include<stdio.h>
#include<string.h>
intmain()
{
charc,str[80];
inti=0,j,k,len;
printf("input:");
gets(str);
len=strlen(str);
c=str[i];
while(c!='