Reverse a given string(string -1) and find number of times the reversed string occurs in another string(string -2)

Sample Input:
doog
Climate is good now

Sample Output:
1

Explanation:
The user is expected to enter 2 strings inputs. The first string would be a string such that if it's reversed would be a meaningful English word. The second input would be an array of strings such that it contains at least one of the reversed string of the first input.

#include <stdio.h>
#include <string.h>
int main(){
        char ch;
        char str1[100];
        printf("Enter the reverse string : ");

        /* scanf("%s",&str1);
         * warning: format %s expects type char *,
         * but argument 2 has type char (*)[100] */

        /* scanf("%[^\t\n]s",str1);
         * scanf to accept multi-word string */

        /* warning : the `gets' function is
         * dangerous and should not be used */

        gets(str1);

        char str2[]="Climate is good now";
        int len=0, i=0, length=0;
        length = len = strlen(str1)-1;
        while(i < len){
                ch = str1[i];
                str1[i] = str1[len];
                str1[len] = ch;
                i++;
                len--;
        }
        printf("The reverse of string is : %s\n", str1);
        int k=0, j=0, count=-1, num=0;
        while(str2[k] != '\0'){
                if (str2[k] == str1[j]){
                        count++;
                        j++;
                        if(count == length){
                                num++;
                                count =0;
                                j=0;
                        }
                }
                k++;
        }
        if(num > 0)
                printf("The string <%s> is present %d times in another string <%s>\n", str1, num, str2);
        else
                printf("The string <%s> is not present in another string <%s>\n", str1, str2);
        return 0;
}

   

Output :
Enter the reverse string : doog
The reverse of string is : good
The string <good> is present 1 times in another string <Climate is good now>

No comments:

Post a Comment