• H面试程序(12): 输出字符串中第一个只出现一次的字母


    题目描述:

                  若字符串str为'' sbdddsbfc'',则输出 f;

                  若字符串str为''aabbccdd'',则输出:字符串str中的字符都出现两次以上

    #include <stdio.h>
    #include<malloc.h>
    #include<memory.h>
    #include<assert.h>
    
    int  find_first_char(char*  str)  
    {  
       assert(str);
       char * phash = (char *)malloc(256*sizeof(char)); //进行一些初始化的工作
    
    
       //此处也可以申请栈的空间,申请字符数组char[256] = {0};作为哈希数组
       assert(phash);
       memset(phash, 0, 256);   //把申请的空间都设为0
    
    
       int i = 0;
       while(str[i] != '')    //将str1在phash数组中映射一遍,对应的字符每出现一次就加1
       {
    	   phash[str[i]] += 1;   
           i++;
       }
    
    
        i =  0;
    	while(str[i] != '')  //第二次扫描,按字符串的顺序,找到第一个在字符串出现一次的字符
        {
    		if(phash[str[i]] == 1)
    		   return i ;
    		i++;
    	}
    
    	if(str[i] = '')  //整个字符扫描结束后,都没发现,返回0,代表字符串中的所有字符都出现两次以上
    		   return 0;
    		 
    	
    }
    int main()
    {
    	char str[] ="aabbdcc";   
    	
    
    	int a = find_first_char(str); //a为字符串中第一个出现一次的字母在数组中的位置,0代表没有这样的字符
    	if(0 ==a)  
    		printf("字符串str中的字符都出现一次以上!!!
    ");
    	else
    	    printf("%c
    ",str[a]); 
    	
    	return 0;
    
    }  


     

  • 相关阅读:
    二叉树的遍历
    深度优先遍历和广度优先遍历
    N的阶乘末尾有多少个0
    框架产生的历史
    Ansible--初始ansible
    日积跬步05
    日积跬步04
    日积跬步03
    日积跬步02
    日积跬步01
  • 原文地址:https://www.cnblogs.com/pangblog/p/3270908.html
Copyright © 2020-2023  润新知