C语言 输入一个字符串 去掉重复的字符,输出无重复的

C语言 输入一个字符串 去掉重复的字符,输出无重复的,第1张

统计字符中的重复字符,可以通过简单哈希算法来实现。
有效的字符的ascii值在0-127之间,定义一个128位的数组,初始化为0,用数组下标对应相应的字符,数组元素值表示字符出现的频率,统计相应字符的个数,个数大于1的字符就是重复的。
参考代码如下:
#include
void main()
{
char s[1024];
int carr[128]={0};
int i;
printf("input a string: ");
gets(s);
for( i=0;s[i];i++ )
{
int index=s[i];
if ( index >=0 && index <= 127 ) //安全处理,防止输入异常时,程序出错
carr[index]++ ;
}
for( i=0;i<128;i++ )
if ( carr[i] > 1 )
printf("%c:%d\n", i, carr[i] );
}运行结果:
input
a
string:
hello
world
l:3
o:2

import javautilArrayList;
import javautilList;
public class $ {
    public static void main(String[] args) {
        String str = "教练教练裁判裁判裁判运动员运动员";
        List<String> data = new ArrayList<String>();
        for (int i = 0; i < strlength(); i++) {
            String s = strsubstring(i, i + 1);
            if (!datacontains(s)) {
                dataadd(s);
            }
        }
        String result = "";
        for (String s : data) {
            result += s;
        }
        Systemoutprintln(result);
    }
}

教练裁判运动员

字符串的Replace()方法应该可以帮你:(返回均是字符串)
string str=textBoxTextReplace("111", "ggg");//替换字符串,也可以是一个字符,但是需要用双引号
string str= textBoxTextReplace('f','g');//替换单个字符
试试吧

;//获取差集    public String getDifferenceSet(){        String ds = "";//差集字符串        boolean isDifference=false;//是否相同                  //如果one的长度小于two的长度  就交换one和two        if(onelength() < twolength()){            String str = one;            one = two;            two = str;        }                  for (int i = 0; i < onelength(); i++){            for(int j = 0; j < twolength(); j++){                //如果相等  那么跳出循环                if(onecharAt(i) == twocharAt(j)){                    isDifference=false;                    break;                }                //否则继续判断                else{                    isDifference=true;                }            }                          //如果不相同  就加入到ds中            if(isDifference){                ds += "" + onecharAt(i);                isDifference = false;            }        }                  return ds;    }

效果图如下:

展开全部
C语言自定义函数,一次性去除连续重复字符,参考代码如下:
#include
char
fun(char
str)
{
int
i=1,j=0;
if(str==NULL)
return
NULL;
if(str[0]==0)
return
str;
while(str[i]!=0)
if(str[j]!=str[i])
str[++j]=str[i++];
else
++i;
str[j+1]=0;
return
str;
}
int
main()
{
char
str[500];
gets(str);
puts(fun(str));
return
0;
}

代码如下:

create or replace function remove_rame_string(oldStr varchar2, sign varchar2)  
  return varchar2 is  
  
  /  
   Oracle去掉重复字符串  
   函数名称:RemoveSameStr  
   参    数:名称         类型       说明  
             oldStr           varchar2       要处理的字符串  
             sign             varchar2       字符串分隔符  
   返 回 值:Result           varchar2       不包含重复子串的记录  
  /  
  str          varchar2(2000);  
  currentIndex number;  
  startIndex   number;  
  endIndex     number;  
  
  type str_type is table of varchar2(30) index by binary_integer;  
  arr str_type;  
  Result varchar2(1000);  
begin  
  -- 空字符串  
  if oldStr is null then  
    return('');  
  end if;  
  
  --字符串太长  
  if length(oldStr) > 2000 then  
    return(oldStr);  
  end if;  
  str := oldStr;  
  
  currentIndex := 0;  
  startIndex   := 0;  
  
  loop  
    currentIndex := currentIndex + 1;  
    endIndex     := instr(str, sign, 1, currentIndex);  
    if (endIndex <= 0) then  
      exit;  
    end if;  
  
    arr(currentIndex) := trim(substr(str,  
                                     startIndex + 1,  
                                     endIndex - startIndex - 1));  
    startIndex := endIndex;  
  end loop;  
  
  --取最后一个字符串:  
  arr(currentIndex) := substr(str, startIndex + 1, length(str));  
  
  --去掉重复出现的字符串:  
  for i in 1  currentIndex - 1 loop  
    for j in i + 1  currentIndex loop  
      if arr(i) = arr(j) then  
        arr(j) := '';  
      end if;  
    end loop;  
  end loop;  
  
  str := '';  
  for i in 1  currentIndex loop  
    if arr(i) is not null then  
      str := str || sign || arr(i);  
      --数组置空:  
      arr(i) := '';  
    end if;  
  end loop;  
  
  --去掉前面的标识符:  
  Result := substr(str, 2, length(str));  
  
  return(Result);  
end remove_rame_string;

public static void main(String[] args) {
String str="r1qwerty";
String str1="qqwer1z";
String[] check = new test01()check(str,str1);
Systemoutprintln("str: "+check[0]);
Systemoutprintln("str1: "+check[1]);
}
public String[] check(String str,String str1){
char[] c = strtoCharArray();
char[] c1 = str1toCharArray();
StringBuffer s=new StringBuffer();
StringBuffer s1=new StringBuffer();
for(int i=0;i<clength;i++){
for(int j=0;j<c1length;j++){
if(c[i]==(c1[j])){
str=strreplaceAll(StringvalueOf(c[i]), "");
str1=str1replaceAll(StringvalueOf(c[i]), "");
}
}
}
return new String[]{str,str1};
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13349845.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-19
下一篇 2023-07-19

发表评论

登录后才能评论

评论列表(0条)

保存