Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C++
#include<bits stdc++.h="">

using namespace std;

#define MAX_CHAR 256

int findRepeatFirst(char* s)
{

	int p = -1, i, k;

	int hash[MAX_CHAR] = { 0 };


	int pos[MAX_CHAR];

	for (i = 0; i < strlen(s); i++) {
		k = (int)s[i];
		if (hash[k] == 0) {
			hash[k]++;
			pos[k] = i;
		} else if (hash[k] == 1)
			hash[k]++;
	}

	for (i = 0; i < MAX_CHAR; i++) {
		if (hash[i] == 2) {
			if (p == -1)
				p = pos[i];
			else if (p > pos[i])
				p = pos[i];
		}
	}

	return p;
}


int main()
{
	char str[MAX_CHAR];
	cin>>str;
	int pos = findRepeatFirst(str);
	if (pos == -1)
		cout << "YES"<<endl;
 else
   cout << "no"<<endl;
 return="" 0;
}


What I have tried:

C++
#include<bits stdc++.h>

using namespace std;

#define MAX_CHAR 256

int findRepeatFirst(char* s)
{

	int p = -1, i, k;

	int hash[MAX_CHAR] = { 0 };


	int pos[MAX_CHAR];

	for (i = 0; i < strlen(s); i++) {
		k = (int)s[i];
		if (hash[k] == 0) {
			hash[k]++;
			pos[k] = i;
		} else if (hash[k] == 1)
			hash[k]++;
	}

	for (i = 0; i < MAX_CHAR; i++) {
		if (hash[i] == 2) {
			if (p == -1)
				p = pos[i];
			else if (p > pos[i])
				p = pos[i];
		}
	}

	return p;
}


int main()
{
	char str[MAX_CHAR];
	cin>>str;
	int pos = findRepeatFirst(str);
	if (pos == -1)
		cout << "YES"<
Posted
Updated 17-Mar-21 3:51am
v2
Comments
Rick York 17-Mar-21 12:08pm    
NO!

1 solution

C doesn;t support streams: so cin and cout don't exist.
Use printf and scanf instead.

The logic we can't do anything about: we have no idea what you expect a change of logic to do, and there is little enough logic in that code to start with ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900