Learn Computer Science programming - C# and C++ Algorithms

 

First Non-Repeat Character

Searches a string for any repeated characters and returns the first character it finds that is unique.

Code
firstnonrepeatedchar.zip
File Size: 6620 kb
File Type: zip
Download File

#include <iostream>
#include <string.h>
#include <windows.h>
using namespace std;


char *FirstNonRepeatedChar (char *pArray)
{
int y; int value;
//char a[] = "TaXbccaT";
char *HashTable = new char[255];
ZeroMemory(HashTable, 255);
wcout << "The array is: " << pArray << endl;

for (int i=0; i < strlen(pArray); i++)
{
value = HashTable[(int)(pArray[i])] + 1;
HashTable[(int)(pArray[i])] = value;
wcout << endl << "ASCII: "  << (int)(pArray[i]) << "  char:  " << pArray[i] << " - Set Hashtable element: " << (int)(pArray[i]) << " with value " << value;
}
wcout << endl << endl << "Looping through Hashtable" << endl;
for (int j=0; j < strlen(pArray); j++)
{
if (HashTable[(int)(pArray[j])] == 1) 
{
wcout << endl << "First Non Repeated Char = " << pArray[j] << " - Value = " << (int)(HashTable[(int)(pArray[j])]) << endl;
return & pArray[j];
}
}
}

void main()
{
int x;
// -----------------------------------------------------------------------
wcout <<  endl << "*** Hashtable - First non repeated character ***" << endl;
wcout << "*************************************************" << endl;
char *pArray = "TaXbccaT";
char *character = FirstNonRepeatedChar (pArray);
wcout << "Non Repeated character is: " << *character << endl;
cin >> x;
 }