求助:找出字符串中第一个出现的不重复的字符
rt
[解决办法]
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[])
{
string str;
cout < < "Input a string: ";
cin> > str;
int i;
char c;
string::size_type index;
for(i=0; i <str.length(); i++)
{
c = str[i];
index = str.find(c);
if(index!=i || str.find(c, index+1) != string::npos)
continue;
else
{
cout < < "Find the character: " < <c < < ", index= " < <i < <endl;
break;
}
}
if(i == str.length())
cout < < "All the characters are repeated!! " < <endl;
system( "pause ");
return 0;
}
[解决办法]
#include <stdio.h>
#include <stdlib.h>
不用字典:
#include <string.h>
int main()
{
char *s = "dgsd gjbsbdks dsjkbkkkkbka631413 ",*p = s,*f = strchr(s, '\0 ');
while (*p != '\0 ')
{
if (strchr(s,*p) == p && strchr(p+1,*p) == NULL)
if (p <f)
f=p;
++p;
}
printf( "%c ",*f);
system ( "PAUSE ");
return 0;
}