top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to find first unrepeated character in string using C/C++?

+1 vote
633 views

How to find first unrepeated character in string using C/C++?

Input       Output     
abc         a    
aabcd       b
aabddbc     c
posted Feb 17, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
// C program to find first non-repeating character
#include<stdlib.h>
#include<stdio.h>
#define NO_OF_CHARS 256

/* Returns an array of size 256 containg count
of characters in the passed char array */
int *getCharCountArray(char *str)
{
int *count = (int *)calloc(sizeof(int), NO_OF_CHARS);
int i;
for (i = 0; *(str+i); i++)
    count[*(str+i)]++;
return count;
}

/* The function returns index of first non-repeating
character in a string. If all characters are repeating 
then returns -1 */
int firstNonRepeating(char *str)
{
int *count = getCharCountArray(str);
int index = -1, i;

for (i = 0; *(str+i); i++)
{
    if (count[*(str+i)] == 1)
    {
    index = i;
    break;
    } 
} 

free(count); // To avoid memory leak
return index;
}

/* Driver program to test above function */
int main()
{
char str[] = "aabddbc";

int index = firstNonRepeating(str);
if (index == -1) 
    printf("Either all characters are repeating or string is empty");
else
printf("First Unrepeating character is %c", str[index]);
getchar();
return 0;
}

Output:

First Unrepeating character is c
answer Feb 18, 2016 by Shivaranjini
0 votes
// C program to find first non-repeating character
#include<stdlib.h>
#include<stdio.h>
#define NO_OF_CHARS 256

/* Returns an array of size 256 containg count
   of characters in the passed char array */
int *getCharCountArray(char *str)
{
   int *count = (int *)calloc(sizeof(int), NO_OF_CHARS);
   int i;
   for (i = 0; *(str+i);  i++)
      count[*(str+i)]++;
   return count;
}

/* The function returns index of first non-repeating
   character in a string. If all characters are repeating 
   then returns -1 */
int firstNonRepeating(char *str)
{
  int *count = getCharCountArray(str);
  int index = -1, i;

  for (i = 0; *(str+i);  i++)
  {
    if (count[*(str+i)] == 1)
    {
      index = i;
      break;
    }   
  }  

  free(count); // To avoid memory leak
  return index;
}

/* Driver program to test above function */
int main()
{
  char str[] = "geeksforgeeks";
  int index =  firstNonRepeating(str);
  if (index == -1)  
    printf("Either all characters are repeating or string is empty");
  else
   printf("First non-repeating character is %c", str[index]);
  getchar();
  return 0;
}

Credit: geeksforgeeks

answer Mar 10, 2016 by Ashish Kumar Khanna
Similar Questions
0 votes

Find the first non repeating character in a string.

For example
Input string: "abcdeabc"
Output: "e"

0 votes

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

+2 votes

For
e.g 1. "hello how are you" - distance between "hello" and "you" is 3.
e.g 2. "hello how are hello you" - distance is 1
e.g 3. "you are hello" - distance is -1. Order of "hello" and "you" should be preserved.
e.g 4. "hello how are hello" - distance is -1 since "you" did not occur even once.

...