Click here to Skip to main content
15,747,189 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If a word is not a palindrome, then just print “Not Palindrome” without quotations.
Case #2: If the word is a palindrome and the word is too long if its length is strictly more than 7 characters. All too long words should be replaced with a special abbreviation.
This abbreviation is made like this: We will write down the first and the last letter of a word and between them - we will write the number of letters between the first and the last letters. That number is in the decimal system and doesn't contain any leading zeroes. Thus, "mmmmmabbammmmm" will be spelled as "m12m".

Case #3: If the word is plaindrome and its length is less than or equal 7 characters.Then, just print the word.
Input Format

The first line contains an integer T (1 ≤ T ≤ 5).
Each of the following T lines contains one word S. Where, all the letters are lowercase and possess the lengths of from 1 to 20 characters

What I have tried:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main()
    char s[21];
    int i,n,c=0,t,j;

            printf("Not Palindrome\n");
    return 0;
Updated 18-Nov-22 13:00pm
jeron1 18-Nov-22 12:13pm    
What is it doing (or not doing) versus what is expected?

Personally, I would write a function to determine if a word is a palindrome. Of course, that is optional. Anyway, I think your logic is flawed for that. You only have to check half of the letters in a word. If its length is odd that's OK because you can ignore the middle character. Also - you should try to avoid single letter variable names because they are not helpful to understanding the code. Here's what your code could look like :
length = strlen( str );
halflen = length / 2;

for( i = 0; i < halflen; ++i )
    if( str[ i ] == str[ length - 1 - i ] )
if( count == halflen )   // it was a palindrome

// and so on...
As it turns out, the problem is you are not resetting your character count variable to zero for every string read.
Share this answer
merano99 18-Nov-22 18:40pm    
my 5
In addition to Rick York's solution, you have an issue with the initial scanf() Unfortunately, scanf("%d", &t) does not remove the trailing whitespace after a number (but it does remove leading whitespace!). So when you call gets() in your for loop the first time, you get an empty string - the contents of the input buffer after the number read in, which in this case is probably the newline character.
There's various ways around this. You could call gets() immediately following the scanf(), or perhaps a loop calling getchar(), which ends when it finds a '\n', you could also use scanf("%d\n", &t);, as long as you're confident that the input will always have a newline at the end.
And be careful with gets(). It does not check its input bounds, and will happily write data past the end of the string so if you have
char str[15];
and someone enters "The quick brown fox jumps over the lazy dog", then you get the dreaded Undefined behavior - Wikipedia[^] Its much safer to use fgets() instead.
Share this answer
merano99 18-Nov-22 18:43pm    
my 5
There are obviously some things that don't work as they should.
Rick and k5054 have already mentioned important points, but there would be some more. The gets() function has not been supported by current compilers for several years. Also scanf() has been considered unsafe for a long time. Maybe you should think about a newer compiler. If you solve the problem with the end-of-line character in scanf() as suggested by k5054, gets() should be replaced in any case, since it would almost certainly crash on longer user input. Here, a length-limited input with fgets() would certainly be recommended. Also the missing initialization of the variable c would have to be solved. After that there are also quite a few other errors. The comparison as well as the output could also still be to be changed.
scanf("%d\n", &t);

for (j = 1; j <= t; j++)
  // gets(s);
  fgets(s, sizeof(s) - 1, stdin);
  n = strlen(s) - 1;

  // for (i = 0; i<n; i++)
  for (i=0, c=0; i < n/2; ++i)
  { ...
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900