Practice Program







  • Program to check string is Palindrome or Not.



  • A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar or the number 10801.

  • Solution:
    void main()
    {
        int raise=1 ,i;
    
        char s1[20];
    
    
        printf("Enter the string: ");
        gets(s1);
    
    
    
     int l = strlen(s1);//storing length of string in variable l.
    
    
       for(i=0; i<(l/2); i++)
        {   //checking the character at index i with the character at index (l-1)-i are same or not.
            if(s1[i] != s1[(l-1)-i])//if characters are not same than will assign 0 to variable raise.
            {                   //assigning 0 to variable raise is indication that string is Not palindrome.
                raise = 0;
            }
    
        }
        if(raise == 0)
        {
            printf("String is Not palindrome");
        }
        else
            printf("String is  palindrome.");
    
    }
    

    Output1
    Enter the string: programmerdouts

    String is not palindrome

    Output2
    Enter the string: LeveL

    String is palindrome

    Output3
    Enter the string: racecar

    String is palindrome








    defining a function 'is_palindrome' which will check string is palindrome or Not

    int is_palindrome(char s1[])
    {
         int l = strlen(s1);//storing length of string in variable l.
     int i,raise = 0;
    
    
     char ch;
        for(i=0; i<(l/2); i++)
        {
            if(s1[i] != s1[(l-1)-i])
            {
                return 0;
            }
    
        }
    
    }
    

    Using above define function 'is_palindrome()':
    void main()
    { int result;
        char s[20] = "code";
    
        result  =is_palindrome(s);
    
        if(result == 1)
        {
            printf("String is palindrome");
        }
        else
            printf("String is not palindrome.");
    
    }
    

    Output1
    String is not palindrome

    Using above define function 'is_palindrome()':
    void main()
    { int result;
        char s[20] = "level";
    
        result  = is_palindrome(s);
    
        if(result == 1)
        {
            printf("String is palindrome");
        }
        else
            printf("String is not palindrome.");
    
    }
    

    Output2
    String is  palindrome

    Using above define function 'is_palindrome()':
    void main()
    { int result;
        char s[20] = "programmer";
    
        result  = is_palindrome(s);
    
        if(result == 1)
        {
            printf("String is palindrome");
        }
        else
            printf("String is not palindrome.");
    
    }
    

    Output3
    String is not palindrome