This is a C program to print smallest and biggest possible word which is palindrome in a given string.
This C Program print smallest and biggest possible word which is palindrome in a given string.
Take input from the user and print smallest and biggest possible word which is palindrome in a given string as shown in the program below.
Here is source code of the C Program to print smallest and biggest possible word which is palindrome in a given string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/* * C Program To Print Smallest and Biggest possible Word * which is Palindrome in a given String */ #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { int i = 0, l = 0, j, k, space = 0, count = 0, init = 0, min = 0, max = 0, len = 0, flag; char a[100], b[30][20], c[30], d[30], minP[30], maxP[30]; printf("Read a string:\n"); fflush(stdin); scanf("%[^\n]s", a); for (i = 0;a[i] != '\0';i++) { if (a[i] == ' ') space++; } i = 0; for (j = 0;j<(space+1);i++, j++) { k = 0; while (a[i] != '\0') { if (a[i] == ' ') { break; } else { b[j][k++] = a[i]; i++; } } b[j][k] = '\0'; } for (j = 0;j < space + 1;j++) printf("%s ", b[j]); printf("\n"); for (i = 0;i < space + 1;i++) { strcpy(c, b[i]); count = strlen(b[i]); k = 0; for (l = count - 1;l >= 0;l--) d[k++] = b[i][l]; d[k] = '\0'; if (strcmp(d, c) == 0) { flag = 1; if (init < 1) { strcpy(minP, d); strcpy(maxP, d); min = strlen(minP); max = strlen(maxP); init++; } printf("String %s is a Palindrome\n", d); len = strlen(d); if (len >= max) strcpy(maxP, d); else if (len <= min) strcpy(minP, d); else printf(""); } } if (flag == 1) { printf("The minimum palindrome is %s\n", minP); printf("The maximum palindrome is %s\n", maxP); } else printf("given string has no pallindrome\n"); }
In this C program, we are reading the string to ‘a’ character[] array variable. For loop is used to count the number of space present in between the words. Another for loop is used to assign the string from the value of ‘a’ character variable to b[] character variable. Using while loop check the value of a[i] character variable value is not equal to null. If the condition is true then execute the while loop.
If-else condition statement is used to check the value of ‘character’ variable is equal to empty space. If the condition is true then execute the statement, break command is used to stop iteration of the loop. Otherwise, if the condition is false, then execute the else statement. Assigning the value of a[i] character variable to b[] character variable.
For loop is used to find the smallest and biggest possible words which is Palindrome in a given String. The strcpy() function is used copy the b[] array variable value to ‘c’ variable. Using ‘count’ variable compute the length of the string in b[] array variable.
In another for loop initialize the value of ‘l’ variable as the difference between the values of ‘count’ variable by 1. Check the condition that the value of ‘l’ variable is greater than or equal to 0. Using if condition statement check the strcmp() function value is equal to 0. If the condition is true then execute the statement.
Another if condition statement is used to check the value of ‘init’ variable is less than 1. Strcpy() function is used to copy the value of ‘d’ variable to ‘minp’ variable and to ‘maxp’ variable. The ‘min’ variable is used to compute the length of the value of ‘minp’ variable. The ‘max’ variable is used to compute the length of the value of ‘maxp’ variable.
Nested if else condition statement is used to find the smallest and biggest possible word palindrome in a given string. Check the value of ‘len’ variable is greater than the value of ‘max’ variable. If the condition is true then execute the statement. Copy the value of‘d’ string variable to ‘maxp’ variable. Otherwise, if the condition is false, then execute the else if statement. Check the value of ‘len’ variable is less than or equal to the value of ‘min’ variable.
If the condition is true, then execute the statement. Copy the value of ‘d’ string variable to ‘minp’ variable. If the value of ‘flag’ variable is equal to 1 then print the statement as the minimum and maximum strings in the palindrome. Otherwise, if the condition is false, then execute the else statement and print the statement as the string is not palindrome.
$ cc string14i.c $ a.out Read a string: aba abcba abcdcba bcd aba abcba abcdcba bcd String aba is a Palindrome String abcba is a Palindrome String abcdcba is a Palindrome The minimum palindrome is aba The maximum palindrome is abcdcba $ a.out Read a string: abc abcd abc abcd given string has no pallindrome
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Watch Advanced C Programming Videos
- Check C Books
- Practice Computer Science MCQs
- Practice BCA MCQs
- Check Computer Science Books