#include <iostream> #include <string.h>using namespace std;int Firstnotrepeatingchar (string str) {int n = Str.length (); if (n<1 | | n>10000) return-1; int i = 0; for (; i<n;i++) { int j;int k; for (j=i+1;j<n;j++) { if (Str[i]==str[j]) break ; } for (k=i-1;k>=0;k--) {if (str[i]==str[k]) break;} if ((J<=n | | k>=0) && i>=n-1) return-1;if (j==n && k<0) { return i; }} } thought: I=0 for the initial subscript, the process of i++, when I each to a position, we let j=i+1, start to the back traversal and str[i] Compare//if STR[I]==STR[J] immediately break, and let K=i-1, from the K position to the front of the comparison , if str[k]==str[i] is//immediately break returns, the final judgment results. int main () {string s = "aabccdbd"; Cout<<firstnotrepeatingchar (s) <<endl; return 0;}
C + + to find the first occurrence of a character in a string Poute (Ox-Guest sword refers to offer)