#include <iostream>
#include <cstring>
#include <limits>
int main()
{
constexprchar cstr[] = "introduction";
// initialize an array to hold the count of occurrances
int counts[ std::numeric_limits<unsignedchar>::max() + 1 ] = {0} ; // EDIT
// populate the counts
for( unsignedchar u : cstr ) ++counts[u] ;
// find the first char for which count is equal to 1
for( unsignedchar u : cstr ) if( counts[u] == 1 && u != 0 )
{
std::cout << "first non-repeating char is '" << char(u) << "'\n" ;
break ;
}
}
#include <iostream>
#include <cstring> // required
usingnamespace std;
char a(char* b) // couldn't you think of a saner name for this function?
// or for the variables
// 'a' 'b' 'c' 'd' - what will you do after you reach 'z' ?
// go on to 'aa' 'ab' 'ac' 'ad' ?
{
int c[256] = {0};
int d = strlen(b);
for(int i = 0; i < d; i++) c[b[i]]++;
for(int i = 0; i < d; i++)
if(c[b[i]] == 1) return b[i]; // *** warning: array subscript has type 'char'
// doc: Warn if an array subscript has type char. This is a common cause of error,
// as programmers often forget that this type is signed on some machines.
return 0;
}
int main()
{
char* b = "introduction"; // warning: deprecated conversion from string constant to 'char*'
cout << a(b);
return 0;
}