【华为oj】找出字符串中第一个只出现一次的字符

找出字符串中第一个只出现一次的字符

原型:

bool FindChar(char* pInputString, char* pChar);

输入参数:

char* pInputString:字符串

输出参数(指针指向的内存区域保证有效):

char* pChar:第一个只出现一次的字符

如果无此字符 请输出'.'

#include<stdio.h>  
#include<string>  
#include<iostream>
using namespace std;


char getUniqueCharacter(char*s)
{     char order[1000]={0};
      int counter[1000]={0};
	  int i=0;
	  char temp='a';
	  char*p=s;
	  while(*p)
	    {
	      temp=*p;//p指向的字符赋给temp
		  if(counter[temp]==0)//如果temp没出现过,把第i个次序位置留给temp
		  {
		  order[i++]=temp; }
	  counter[temp]++;//temp出现的次数加1
	  p++;//继续往下搜索
	  
	       }
//遍历字符串来查账count=1的字符
	  int j;
	  for(j=0;j<1000;j++)
		  if(order[j])//如果次序j的值不为0
	 {
		  if(counter[order[j]]==1)//第j个值出现了一次
		  {
		  temp=order[j];
		  break;   //把这个值赋给temp
		  } 
		  
		  else if(counter[order[j]]>1)
		  {  temp='.';}
	 }
		  return temp;
		


}

int main()
{
	char s[1000];
	cin.getline(s,1000);

	char result= getUniqueCharacter(s);
	cout<<result<<endl;

	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值