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:  }  

Write a program to find the longest palindrome in any given string. Ex:forgeeksskeegfor Ans: geeksskeeg

1:  #include <stdio.h>  
2:  #include <stdlib.h>  
3:  #include <string.h>  
4:  int check_palindrom(char *start, char *end)  
5:  {  
6:    if ( ((start+1) == end) || (start == (end-1)) || (start == end))  
7:      return 0;  
8:    if ( *start != *end)  
9:      return -1;  
10:    if ( *start == *end)  
11:    {  
12:      start++;  
13:      end--;  
14:      return check_palindrom(start,end);  
15:    }  
16:  }  
17:  main()  
18:  {  
19:    char *str = "forgeeksskeegfor";  
20:    char *start, *end, *temp1, *temp2;  
21:    int flag = 0,len = strlen(str);  
22:    len--;  
23:    start = str;  
24:      while(*start != '\0')  
25:    {  
26:      end = str + len;  
27:      while(start != end)  
28:      {  
29:        if ( *start == *end)  
30:        {  
31:          if ( 0 == check_palindrom(start,end))  
32:          {  
33:            printf("Yes it <%.*s> is palindrom \n",  
34:                end +1 - start,start);  
35:            flag = 1;  
36:            break;  
37:          }  
38:        }  
39:        end--;  
40:      }  
41:        start++;  
42:        if ( flag == 1)  
43:          break;  
44:    }  
45:  }