Skip to content

Commit 504f7c4

Browse files
committed
First Non-Repeating Character in String
1 parent d25cabd commit 504f7c4

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,8 @@ This is a repository containing various C++ Programs to understand the basic con
102102

103103
C++ Code for finding the second largest element present in the given array.
104104

105+
* [First Non-Repeating Character in an String](https://github.com/altruistcoder/Data-Structures-Python/blob/master/first_non_repeating_character.cpp):
106+
107+
C++ Code for finding the first non-repeating character present in the given string.
108+
109+

first_non_repeating_character.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# include<iostream>
2+
#define NO_OF_CHARS 256
3+
using namespace std;
4+
5+
int firstNonRepeating(char *str)
6+
{
7+
int i;
8+
int *freq = new int[NO_OF_CHARS];
9+
for(i=0; *(str+i); i++)
10+
freq[*(str+i)]++;
11+
cout<<endl;
12+
for(i=0; *(str+i); i++)
13+
{
14+
if(freq[*(str+i)] == 1)
15+
{
16+
return i;
17+
}
18+
}
19+
return -1;
20+
}
21+
22+
int main()
23+
{
24+
char str[256];
25+
cout<<"Enter a string: ";
26+
cin>>str;
27+
int r = firstNonRepeating(str);
28+
if(r == -1)
29+
cout<<"No non-repeating character present in given string. Either all of the characters are repeating or string is empty";
30+
else
31+
cout<<"First non-repeating character present in given string: "<<str[r];
32+
return 0;
33+
}

0 commit comments

Comments
 (0)