String Palindrome Program in C using for loop [New]

In this post, we will make a String Palindrome Program in C using for loop. We will check whether the given string is palindrome or not.
In this post, we will make a String Palindrome Program in C using for loop. We will check whether the given string is palindrome or not.

String Palindrome Program in C using for loop

#include <stdio.h>
#include <string.h>
int main()
{

    char str[30];
    int n = 0, flag = 0, i = 0, j = 0;
    printf("Enter String:");
    scanf("%s", &str);
    n = strlen(str);
    for (i = 0, j = n - 1; i <= j; i++, j--)
    {
        if (str[i] != str[j])
        {
            flag = 1;
            break;
        }
    }

    if (flag == 1)
        printf("No, it is not a palindrome");
    else
        printf("Yes, it is a palindrome");

    return 0;
}


Output:

Enter String: saippuakivikauppias
Yes, it is a palindrome

Enter String: codingwithsid
No, it is not a palindrome


Approach for Palindrome Program in C

  1. Input string from user and store it in a character array.
  2. Using for loop, we compare the corresponding elements of the character array. For this, we use the following statement.
    str[i]==str[j];
  3. We will compare the first letter with the last letter, and second letter with the last-second letter and so on.
  4. If they mismatch, we break the loop and mark the flag with one.
  5. Now, if the flag's value is one, we will print it is not a palindrome; otherwise, the given string is a palindrome.

C program to check whether the string is palindrome or not using while loop

In this program, we will check whether the given string is palindrome or not.
#include <stdio.h>
#include <string.h> int main() { char str[30]; int n = 0, isPalindrome = 1, i = 0, j; printf("Enter String:"); scanf("%s", &str); n = strlen(str); j=n-1; while(i<j) { if(str[i]!=str[j]) { isPalindrome=0; break; } i++; j--; } if (isPalindrome == 0) printf("%s is not a palindrome",str); else printf("%s is a palindrome",str); return 0; }
Output:

Enter String: racecar
racecar is a palindrome

Enter String: souravjoshi
souravjoshi is not a palindrome



How do you check if a String is a palindrome in c?

To check whether the string is a palindrome or not, you can use a  for loop statement which is given below
for (i = 0, j = n - 1; i <= j; i++, j--)
    {
        if (str[i] != str[j])
        {
            flag = 1;
            break;
        }
    }

Where n is the size of the string entered by the user, the above code compares its first, last value, and other array elements to their corresponding elements.

What is a palindrome string example?
Madam
c[0]=c[4]; c[1]=c[3]; c[2]=c[2];


What is the palindrome program in c?
It is a program to check whether the number is a palindrome.
To check palindrome, we have to check its reverse should also equal it.
for example:7227 is a palindrome as its reverse is also 7227
check out this post- palindrome program in c 

Thanks for reading the String Palindrome Program in C, and if you have any queries, you can comment below and contact us on our Instagram account.
Related posts:

Post a Comment

Please do not enter any spam link in the comment box.