Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have written a code to find nth palindrome. It takes single input ('th) from the user. And find the 'th palindrome. I would like to get a review of my code. And any advice to improve it will be happily welcomed. Thank you.

What I have tried:

#include<stdio.h>
int givenValue;
int sizeCounter(int givenValue);
int main(void)
{
    int size, givenValue, th, numofpalindrome=0;
    int i, j, k,notpalindrome ;
    scanf("%d", &th);
    givenValue = 1;
    while(numofpalindrome<th){
            j = givenValue;
            size = sizeCounter(givenValue);
    int array[size];
    i = size -1;
    while(givenValue!=0){
        array[i] = givenValue%10;
        givenValue = givenValue/10;
    i--;
    }
    notpalindrome = 0;
    for(k=0; k<(size/2); k++){
        if(array[k]!=array[size - k - 1]){
            notpalindrome = 1;
            break;
        }
    }
    if(notpalindrome == 0){
        numofpalindrome++;
    }

    givenValue = j;
    givenValue++;
    }
    printf("%d", givenValue-1);
    return 0;
}

int sizeCounter(int givenValue)
{
    int x=0, y;
    y = givenValue;
    while(givenValue!=0){
                givenValue = givenValue/10;
                x++;
    }
    givenValue = y;
    return x;
}
Posted
Updated 24-Jan-22 10:26am

You probably don't want to read this ... code reviews are rarely complimentary ... and this one isn't going to be either.

It looks like "student code" - poorly indented, undocumented, and prone to user error.
There is nothing which tells the user to enter anything - much less what to enter - so he has no idea if the app is loading or waiting for input. When he has guessed that he should type something, unless he guesses you want a number your app will immediately crash on some systems if he types anything else.

You don't seem to understand logical tests - any non-zero value is true in C so there is no need to compare "virtual booleans" with "0".

It's pretty inefficient, and to be honest looks like it was thrown together in a hurry. It isn't modular in any way, and relies on a brute force approach to solving a problem.
 
Share this answer
 
Comments
Jahirul Sarker 24-Jan-22 3:29am    
Actually, it was a brute force approach to solve a previous contest problem and all the variable name was a, b, c...... To post here I gave some name but still haven't changed anything more than that. It's a 50 lines code and took me more than half hours to implement it. So, I am looking for a shorter and less complex method to find nth palindrome.
Jahirul Sarker 24-Jan-22 3:33am    
"any non-zero value is true in C so there is no need to compare "virtual booleans" with "0"."
while(givenValue)? instead of while(givenValue!=0)?
Jahirul Sarker 24-Jan-22 3:38am    
"It looks like "student code" - poorly indented"
I haven't read any books on C. I learned C's basic from a youtube C tutorial class.
I have no theoretical knowledge on C. But this year I admitted to a college with CSE major. I hope I will read some good books.
OriginalGriff 24-Jan-22 4:09am    
That explains a lot: YouTube tutorials are generally total garbage, produced by people who have no idea how to teach, how to make a video, and in most case no idea how to code either ... they are there for "likes and subscribes" because that is where the money is. Mostly, you'll find that they know less that you do, but have grabbed code from somewhere and have no idea how it works.

Stop using code from the internet, read a book (or better go on a course) and try writing your own solution. You will learn a whole load more that way!

I'd also not start with C - it's a very old language, and it's a lot harder to use than modern equivalents. I'd suggest you start with C# which does make things a lot easier, even if it's much more "fully featured" and flexible. It'll also allow you to create Windows apps with a lot less effort than C will!
Jahirul Sarker 25-Jan-22 6:15am    
Sir, I love competitive programming. Shouldn't I go with C++?
You can get it: "Did you read your guidelines?" See Google C++ Style Guide[^]

Ask again when done ;-)

tip: write some tests to verify your results. For that you need to refactor your result by dividing it into input/output and functionality. Like you need a function:
C++
bool isPalindrom(char* word);
which than is testable.
 
Share this answer
 
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include<stdio.h>

int givenValue;
int sizeCounter(int givenValue);
int main(void) {
  int size, givenValue, th, numofpalindrome = 0;
  int i, j, k, notpalindrome;
  scanf("%d", & th);
  givenValue = 1;
  while (numofpalindrome < th) {
    j = givenValue;
    size = sizeCounter(givenValue);
    int array[size];
    i = size - 1;
    while (givenValue != 0) {
      array[i] = givenValue % 10;
      givenValue = givenValue / 10;
      i--;
    }
    notpalindrome = 0;
    for (k = 0; k < (size / 2); k++) {
      if (array[k] != array[size - k - 1]) {
        notpalindrome = 1;
        break;
      }
    }
    if (notpalindrome == 0) {
      numofpalindrome++;
    }

    givenValue = j;
    givenValue++;
  }
  printf("%d", givenValue - 1);
  return 0;
}

int sizeCounter(int givenValue) {
  int x = 0, y;
  y = givenValue;
  while (givenValue != 0) {
    givenValue = givenValue / 10;
    x++;
  }
  givenValue = y;
  return x;
}

Indentation style - Wikipedia[^]
Best C++ Formatter and Beautifier[^]
Online C/C++ Formatter, Indenter and Beautifier – Techie Delight[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]

Advice: Learn to separate concerns. Making a function for nthpalindrome(int)
will help to make clearer each variables usage.
 
Share this answer
 
v2

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



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