First Non-Repeating character

How to show First Non-Repeating character?
"r"

#include <iostream>
#include <string.h>
#define MAX 100

using namespace std;

void isstring(){
char str[MAX] = "introduction";
int checker = 0;

for (int i = 0; i < strlen(str); ++i)
{
int val = str[i] - 'a';
if ((checker & (1 << val)) > 0)
{
printf("String contains duplicate characters\n");
return;
}
checker |= (1 << val);
}
printf("String contains all unique characters\n");
}

int main(){
isstring();
system("pause");
return 0;
}
if ((checker & (1 << val)) > 0)
{
printf("String contains duplicate character %c\n", str[i] );
return;
}

EDIT: Oh, I am sorry. I showed how to show a repeating character.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstring>
#include <limits>

int main()
{
    constexpr char cstr[] = "introduction";

    // initialize an array to hold the count of occurrances
    int counts[ std::numeric_limits<unsigned char>::max() + 1 ] = {0} ; // EDIT

    // populate the counts
    for( unsigned char u : cstr ) ++counts[u] ;

    // find the first char for which count is equal to 1
    for( unsigned char u : cstr ) if( counts[u] == 1 && u != 0 )
    {
        std::cout << "first non-repeating char is '" << char(u) << "'\n" ;
        break ;
    }
}

http://ideone.com/7NNgHC
Last edited on
I already use array and hash table.
And now I want to know about,
How to using Bit Vector?
Use two arrays instead of one. One to flag presence, the other to flag repeats.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <limits>
#include <bitset>

int main()
{
    constexpr char cstr[] = "introduction";

    std::bitset< std::numeric_limits<unsigned char>::max() + 1 > present ;
    std::bitset< std::numeric_limits<unsigned char>::max() + 1 > repeating ;
    for( unsigned char u : cstr )
    {
        repeating[u] = present[u] ;
        present[u] = true ;
    }

    for( unsigned char u : cstr ) if( present[u] && !repeating[u] && u != 0 )
    {
        std::cout << "first non-repeating char is '" << char(u) << "'\n" ;
        break ;
    }
}

http://ideone.com/6e17pQ
Last edited on
How about this code, Is it same?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;
char a(char* b){
        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];
        return 0;
}

int main(){
char* b="introduction";
cout << a(b);
return 0;
}
> How about this code?

Try running the program with the string "intro\0x80duction" as input to the function a

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <cstring> // required
using namespace 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;
}


I do appreciate your kindness.

I am looking for using Bit Vector.
we speak of characters, but not for the rest of input types.
And improve O(N)
Topic archived. No new replies allowed.