Adding a letter in a char Array without duplications

Hello,
i have to read in a word, it should come in a char Array. But if there is a letter twice, e.g. password it only should read in pasword. I tried it with for and if grinds, but it doesn´t worked. I tried:
Schluessel is the char with the entered word, hear password
in 'key' should come the new word, hear pasword
for(int i=0;Schluessel[i]!='\0';i++){
for(int j=0;key[j]='\0';j++){
if(Schluessel[i]!=key[j]){
key[j]=Schluessel[i];
cout<<key[j];
}

}
}
It doesn´t worked, because of the emty char 'key'. i tried it with the lenght of Schluessel but it doesn´t worked.
i´ll be happy if someone could help me.
Why don't you just use a string?
Example of how you can do it with a std::string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        std::string theWord = { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' };

	// Loop through the string and delete a duplicate char
	char prevCharacter = ' '; // This will keep track of the last iterated char
	for (int i = 0; i < theWord.size(); i++)
	{
		if (prevCharacter != ' ')
		{
			if (theWord[i] == prevCharacter) // Duplicate char found
			{
				theWord.erase(theWord.begin() + i, (theWord.begin() + i) + 1);
			}
		}
		prevCharacter = theWord[i];
	}

	std::cout << "The non-duplicate word: " << theWord << std::endl;
Thanks,
i don´t use them, because of my Prof. he said "only use char and int for the task."
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
26
27
28
29
30
31
32
#include <iostream>
#include <iomanip>

bool contains( const char cstr[], char c )
{
    if( cstr != nullptr )
    {
        for( int i = 0 ; cstr[i] != 0 ; ++i ) if( cstr[i] == c ) return true ;
    }

    return false ;
}

int main()
{
    const int MAX_SIZE = 100 ;

    char cstr_in[MAX_SIZE] {} ;
    char cstr_unique[MAX_SIZE] {} ; // value initialised (all null characters)
    int curr_size = 0 ;

    std::cout << "word (max " << MAX_SIZE-1 << " characters)? " ;
    std::cin >> std::setw(MAX_SIZE) >> cstr_in ;

    for( int i = 0 ; cstr_in[i] != 0 ; ++i )
    {
        if( !contains( cstr_unique, cstr_in[i] ) ) cstr_unique[ curr_size++ ] = cstr_in[i] ;
    }

    std::cout << "\n       you entered the string: " << std::quoted(cstr_in) << '\n'
              << "string with unique characters: " << std::quoted(cstr_unique) << '\n' ;
}

http://coliru.stacked-crooked.com/a/1b3fe1b271583a07
closed account (48T7M4Gy)
Or,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
	char alphabet[90] = { 0 }; // <--

	char word[] = "pOasZswo!oOzord!_PasASOzwZoo//Rd_2000!";
	int length = sizeof(word) / sizeof(char);
	int index = 0;

	for (int i = 0; i < length; i++)
	{
		index = word[i] - '!'; //<--

		if (alphabet[index] < 1)
			std::cout << word[i];
		alphabet[index]++;
	}

	return 0;
}


pOasZwo!zrd_PAS/R20 
Exit code: 0 (normal program termination)
Last edited on
> Does it work with uppercase?

Check it out. It is asinine code.

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
26
#include <iostream>

int main()
{
    char alphabet[26] = { 0 };

	char word[] = "BOO!";
	int length = sizeof(word) / sizeof(char);
	int index = 0;

	for (int i = 0; i < length; i++)
	{
		index = word[i] - 'a';
        
        /////////////////   added ////////////////////
        if( index < 0 || index >= int( sizeof(alphabet) ) ) 
            std::cout << "\nabout to access char at alphabet[" << index << "] (undefined behaviour)\n" ;
        /////////////////   added ////////////////////

		if (alphabet[index] < 1)
			std::cout << word[i];
		alphabet[index]++;
	}

	return 0;
}


about to access char at alphabet[-31] (undefined behaviour)
B
about to access char at alphabet[-18] (undefined behaviour)
O
about to access char at alphabet[-18] (undefined behaviour)

about to access char at alphabet[-64] (undefined behaviour)

about to access char at alphabet[-97] (undefined behaviour)

http://coliru.stacked-crooked.com/a/779c5aa530757e9b
closed account (48T7M4Gy)
To answer your question lechuga2000 my program handles uppercase and beyond by adjusting the size of the alphabet (currently 26 on line 5) and starting point ( currently 'a' on line 13) character in the ASCII table. You can probably see this without me demonstrating. We could make it the whole of the ASCII table but that doesn't seem to show how it works without comments, my preference.

Cheers :)

Last edited on
closed account (48T7M4Gy)
I think the ASCII table is fairly universal, well at least where humans like me are concerned. You would know there aren't too many problems there. :)

As we can see, the scale of the problem shouldn't be too much of a problem. There are not many characters involved in even the longest passwords and key phrases around and considering the full ASCII table is about 200 characters or whatever, the test time is minuscule even if your best computer is a TRS80.

You might like to let me know how my program goes after you modify it for user input. My guess is the test would be easily quicker than keystroke time. It obviously wouldn't be good if the time was substantial and interfered, but I haven't tried it. I'll leave it to you if you're interested. I have other commitments unfortunately.

Excellent points you raise. Best wishes.
Cheers :)
closed account (48T7M4Gy)
LOL, we'll need to test it with German umlauts lechuga. You're coming up with the sort of question an experienced testing team would need to address. Whole character sets would need your scrutiny, Greek, Hebrew, you name it.

On the readability question you so ably have turned your mind to I am confident at least in principle kanji and Chinese characters would be manageable. As you would no doubt be fully aware we could say readability is a local issue. I'm limited to English, Spanish, Mandarin and Thai but who knows what languages my program needs to be applied to. Just a couple of simple adjustments I suspect. You could check those out for me too.

Good and thoughtful questions again.
Cheers and best wishes :)
closed account (48T7M4Gy)
So many languages so little time. เวลาผ่านไปอย่างรวดเร็ว

Thoughtfully philosophical points you raise lechuga.
Cheers and best wishes :)
closed account (48T7M4Gy)
是的,它的工作原理与中国人也
closed account (48T7M4Gy)
A internet nunca esquece :)
closed account (48T7M4Gy)
Si
closed account (48T7M4Gy)
You'll notice I left out the german keyboard characters in my revision above which dispels most if not all uncertainty about its flexibility.

You raise an interesting point again but I have no knowledge on being a native english speaker where umlauts are not de rigueur. We live comfortably without them although I find French cedillas somewhat engaging and characteristically stylish. Alas I don't use them either. Yahoo might, can I suggest you check and report back to me on that.

Does yahoo in the fatherland, or whatever they call it these days, accept passwords with alt+nnn characters? These are deep questions my otherwise universal program has not managed to address but given its sophistication and your diligent testing and appraisal I am sure it can prevail in normal password security regimes in the world outside the germanic tribal areas, if not within.

Regional cheers and greetings,
Best of good wishes:)
Last edited on
closed account (48T7M4Gy)
Selamat malam
closed account (48T7M4Gy)
Nyet
Do you fellas suppose you could stop bumping this (now off-topic) thread to the top of the pile?
Topic archived. No new replies allowed.