如何使用c在字符串中找到第一个非重复字符 [英] How do i find the first non repeating character in as string using c

查看:206
本文介绍了如何使用c在字符串中找到第一个非重复字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试的代码不适用于第一个非重复字符出现在字符串末尾的情况。



PS:这不是家庭作业我正在尝试自己学习数据结构和算法。



我尝试过:



The code i tried is not working for the cases when the first non repeating character comes at the end of string.

PS: This is not a homework assignment.I am trying to learn data structures and algorithms by myself.

What I have tried:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{ 
    clrscr();
    int i=0,j,l,flag=0,count=1;
    char str[50];
    printf("\n enter  string");
    gets(str);
    printf("\n");
    puts(str);
    l=strlen(str);
    while(str[i]!='\0'&& count!=0)
    {
        j=i+1;
        while(str[i]!=str[j] && str[j]!='\0')
        j++;

        if(j==l)
        flag=1;
        if(flag==1)
        {
            printf("\n the first unrepeated character is %c",str[i]);
            count=0;
        }
        i++;
    }
    getch();
}

推荐答案

首先,不要使用 count 来强制退出循环 - 删除它并使用中断而不是找到非重复。 (并且不要调用变量 count 除非它们计算的东西 - 但事实并非如此,它是一个bool,它说发现非重复,所以请恰当地命名)。

现在想想它为什么找不到它:在这个序列中它后面是什么字符:

Firstly, don't use count to force an exit from the loop - delete it and use a break instead when you find a non-repeat. (and don't call variables count unless they count things - that doesn't, it's a bool which says "found non-repeat" instead, so name it appropriately).
Now think about why it doesn't find it: what character is after it in this sequence:
AABBC



使用调试器快速运行会告诉你它为什么找不到它!

而不是你使用的方法,我会对输入进行排序: ABSCADSABC变为AABBCCDSS,此时它只是单次通过字符串而D是显而易见的。


A quick run with the debugger would tell you why it doesn't find it!
Instead of the method you are using, I'd sort the input: "ABSCADSABC" becomes "AABBCCDSS" at which point it's just a single pass through the string and the "D" is obvious.


试试这个:

Try this:
// repeat for entire string
while(str[i] != '\0')
{
    if (str[i]!=str[i+1] && str[i+1]!='\0')
    {
        // not the last character, and the two do not match
        // if it is the first character display it
        if (i == 0)
            printf("\n the first unrepeated character is %c\n", str[i]);
        else // display the non-matching one
        printf("\n the first unrepeated character is %c\n", str[i+1]);
        break;  // exit the loop when a non-match is found
    }
    i++;
}


这是相当野蛮的力量,但它应该可以正常工作。

This is rather brute force, but it should work as you want.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    clrscr();
    int i,j,count;
    char str[50];
    printf("\n enter  string");
    gets(str);
    printf("\n");
    puts(str);
    for (i=0;str[i]!='\0', i++)
    {
        // count number of occurrence for current char
        count=0;
        for (j=0;str[j]!='\0',j++)
        {
            if (str[i]==str[j])
            {
                count++;
            }
        }

        if(count==l)
        {
            printf("\n the first unrepeated character is %c",str[i]);
            break;
        }
    }
    getch();
}


这篇关于如何使用c在字符串中找到第一个非重复字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆