top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Find the first non repeating character in a string using C/C++?

0 votes
763 views

Find the first non repeating character in a string.

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

posted May 25, 2017 by anonymous

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

2 Answers

0 votes

// first non repating characeter

include<stdio.h>

include<string.h>

bool foward(char str[],int i,int lenght)
{
int j = i+1;
while( j != lenght)
{
if(str[i] != str[j])
{j++;}
if(str[i] == str[j]){return false;break;}
}
printf("%c ",str[j]);
if(j == lenght)
{return true;}
}

bool reverse(char str[],int i)
{
int j = i -1;
if( j < 0){return false;}
while( j != 0)
{
if( str[i] == str[j]){return false ;break;}else{j--;}

}
if(j == 0){return true;}
}

int main(void)
{
char str[] ={"acaacnaacacaclmlacacacnacac"};

int nmb_of_chars;
int i=0,j = 1;
bool condition = false;

nmb_of_chars = strlen(str);

while (i != nmb_of_chars)
{
condition = foward(str,i,nmb_of_chars);
if(condition == false)
{i++;}
else
{
condition = reverse(str,i);
if(condition == true)
{printf("-------%c-----",str[i]);break;}
else{i++;}
}
}

if(i == nmb_of_chars){printf("There is no non repeating charactears");}

getchar();
getchar();
return 0;}
answer May 26, 2017 by Leon Martinović
first in your example is d and sry i am not gona coment this code
0 votes
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
        if(argc != 2)
        {
                printf("Provide string as an command line argument.\n");
                exit(1);
        }
        int i,j;
        for(i;argv[1][i];i++)
        {
                for(j=i+1;argv[1][j];j++)
                {
                        if(argv[1][i]==argv[1][j])
                        {
                                printf("1st repeating character is : %c\n",argv[1][i]);
                                exit(1);
                        }
                }
        }
        printf("No repeating characters found....\n");
        return;
}
answer May 26, 2017 by Chirag Gangdev
Similar Questions
+1 vote

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

Input       Output     
abc         a    
aabcd       b
aabddbc     c
0 votes

Write the prototype function in C: char * my_strchr (char * arr, char c);

The function should return the cursor to the first occurrence of the forwarded character in the character sequence or NULL if the forwarded character was not found.

i came up with solution also but it has to be string :

char *my_strchr(char *arr,char c)
{
int i=0;
char *p;
while(1)
{
if(i[arr] == c){p = i[arr];return p;break;}
if(i[arr] == '\0'){p= NULL;return p;break;}
i++;
}}

...