Search This Blog

Tuesday, October 20, 2015

write a function which returns the occurrence of first non-repetitive character from a given string. Function will return space(' ') if there is no such character found. And characters are case sensitive 'a' is not equal to 'A'.

1:  #include <stdio.h>  
2:  #include <stdlib.h>  
3:  char check_for_first_non_repetative_char(char *str)  
4:  {  
5:    int i = 0,arr[256] = { 0};  
6:    char *temp = str;  
7:    while(*temp != '\0')  
8:    {  
9:      arr[*temp]++;  
10:      temp++;  
11:    }  
12:    temp = str;  
13:    while(*temp != '\0')  
14:    {  
15:      if( arr[*temp] == 1)  
16:        return *temp;  
17:      temp++;  
18:    }  
19:    return (' ');  
20:  }  
21:  main()  
22:  {  
23:    char str[4][100] = {  
24:      "amazon","alibaba","facbook","google"};  
25:    char ch = '\0';  
26:    int i =0 ;  
27:    for(i=0;i<4;i++)  
28:    {  
29:      ch = check_for_first_non_repetative_char(str[i]);  
30:      if ( ch != ' ')  
31:      {  
32:        printf("First Non-Repeatating character '%c' in string \"%s\"\n",ch,str[i]);  
33:      } else {  
34:        printf("No non-repetative charecter in string \"%s\"\n",str[i]);  
35:      }  
36:    }  
37:    exit(0);  
38:  }  

No comments:

Post a Comment