So I need to write a code that prompts the user to enter a 5-8 digit number and i have the following guidelines,
1. The password must be between 5 and 8 digits long (assume no leading zeros are entered – for example: 002345 is considered only 4 digits for our purposes).
2. The last five digits of the password must be a palindrome (i.e. the same backwards as it is forwards, such as 52425).
3. The last three digits of the password must be a prime number (i.e. if the password entered was 730103, 103 is prime).

so far i have some of it down but I'm having trouble with the palindrome part, can anyone explain how to split up a number and then check for palindrome.
also, this is what i have so far

Code:
```#include <stdio.h>
int main(void) {

int pass = 0;
int counter = 0;
int num1 = 0;
int num2 = 0;
int num3 = 0;
int num4 = 0;
int num5 = 0;

printf("Enter a password between 5 and 8 digits.\n");
scanf("%d",&pass);

while(pass){
pass=pass/10;
counter++;
}
if(!((counter<=8)&&(counter>=5))){
printf("Password is invalid, must be between 5 and 8 digits.\n")
}else{
if(){                                                         //check for palindrome with ! at beginning
printf("Password is invalid, last 5 digits are not a palindrome")

}else{

}

}

return 0;
}```

2. Also, I have all those numbers defined because I'm assuming that is the start to separating the numbers, is that correct?

3. I would read the whole password in as a string (array of char), using something like fgets (note, fgets leaves the newline, so you need to get rid of it -- suggestions here). That is most robust for handling invalid input (scanf with %d will give you problems if they enter letters, space, punctuation, etc). It also gives you the most flexibility for processing later.

Some of the checks (like all-digits, length, leading zeros and palindrome) are easiest done on an array of digits, which is basically what you have in a string, each char in that string is one of the digits. For example, the palindrome check, with an array, is simply working your way from the two ends toward the middle. Checking primality of the last 3 digits is easier if you are using an integer type. Thankfully, 5-8 digit numbers fit nicely in a 32-bit integer, so you can safely store any valid password in a long int. Use the strtol() function to convert the string to a long. Make sure you read the documentation and cover all error cases. Then you can use % (modulo/remainder) to get the last 3 digits as a single number, and check that for prime.

4. Thanks for that. Another question I have is whether or not putting 8 parts to check will affect anything if a user decides to only use 5, 6, or 7 numbers.
Code:
```  char pass[SIZE];

pass[0]=0;
pass[1]=0;
pass[2]=0;
pass[3]=0;
pass[4]=0;
pass[5]=0;
pass[6]=0;
pass[7]=0;```
this is what I'm talking about

5. Also, how can I make the program only read the last 5 digits to check for palindrome in the case someone enters more than 5 digits?

6. No, you can use strlen to find out how many characters they actually entered. Remember though, to read the link in my previous post (the red text), about the fgets and removing the newline. Make sure you do that first thing after you call fgets, before you check the length.

Do something like:
Code:
```fgets(pass, sizeof(pass), stdin);
// remove newline
int pass_len = strlen(pass);
check length
check strtol -- if errors, the user entered non-digits (letters, spaces, punctuation, etc)
check palindrome
check prime of last 3 digits```
EDIT: Note, you should check the return value of fgets to make sure it worked too, I left that off since this was a simple example. Always check whether a function succeeded, and act appropriately, before you let your program continue.

7. Originally Posted by jgkamin
Also, how can I make the program only read the last 5 digits to check for palindrome in the case someone enters more than 5 digits?
You should not actually be writing any code right now. You don't seem to have a full grasp of the problem or how to solve it all. If you can't do it yourself, how can you expect to program a computer to do it correctly? Try this out with paper and pencil first, using several examples, some with bad input (non-numeric, wrong length, etc). Take careful notice of how you perform each step on paper, those steps will be the basis for your algorithm.

For example, someone enters a password of length 8, say "13584629". That means you have pass[0] ("1") through pass[7] ("9"). The last 5 digits are "84627". What indexes do those correspond to? pass[?] through pass[7]. How do you determine the start index of the final 5? How do take a total length 8, and a "substring" length 5, and arrive at that starting index?

8. So if I'm using fgets, I can't use any int's to define variables or can I?

9. Originally Posted by jgkamin
So if I'm using fgets, I can't use any int's to define variables or can I?
Sure you can, but you will use a char array to read into. The ints (or longs in this case) will be used a bit later, for the result of strtol and the prime check. Note, you will want SIZE to be bigger than 8, you need room for the null terminator at least, and possibly a new line that fgets appends. I would make it big enough to contain a "reasonably" large line of input from the user, like 256, just to be safe. Then you #define MIN_LEN 5 and #define MAX_LEN 8 for your length checks.

10. Well I'm just not familiar with fgets as that hasnt been covered in class. Is there any way to use the format I started with because I understand that a lot better. Sorry If I'm making this difficult for you.

11. No worries, you're not making it difficult on me. You could use the format you started with. I just have no way of knowing what you have and haven't covered in class, nor do I know the complete requirements of your assignment. Start with providing the exact requirements. Either copy-paste them from the document, or provide a link to the web page with the assignment.

Whether using scanf is practical depends on what types of input you may be given. If you are supposed to handle non-numeric input, or very long sequences of digits, then scanf is not a good choice. If you are guaranteed to only have numeric input, and nothing more than 8 characters, then scanf would work. It really depends.

EDIT: Also, some teachers are strict about only using stuff you learned in class, while other teachers love that you took the initiative to find a different (possibly better) solution.

12. Ok so this is the whole program assignment
You are an analyst at the Central Intelligence Agency (CIA). You are assigned the job of developing a program that will analyze passwords that are used to encrypt password-protected data. The CIA uses a very specific set of rules for passwords to identify non-CIA operatives who attempt to access password-protected data (these non-CIA operatives will try passwords that do not conform to CIA rules). The rules are complicated enough that the CIA has assigned you the job of developing a program to test whether a password meets their rules. CIA personnel will then use the program to make sure the passwords they select to protect data conform to the rules. The rules that your program must use to determine if the password is valid, are as follows:

1. The password must be between 5 and 8 digits long (assume no leading zeros are entered – for example: 002345 is considered only 4 digits for our purposes).
2. The last five digits of the password must be a palindrome (i.e. the same backwards as it is forwards, such as 52425).
3. The last three digits of the password must be a prime number (i.e. if the password entered was 730103, 103 is prime).

If the password entered follows the above rules, you must report that the password is valid. If the password does not meet the rules, then your program should report that the password is invalid, and print out at least one reason why it is not valid. Your program must continually ask for a new password until a sentinel value is entered. The sentinel value will be negative one (-1). You may not prompt the user for the individual digits of the password.

NOTE: Your program must also validate user input. A valid user input is considered to be a positive integer.

and yes the inputs are purely numerical, and the guidelines are to have a password between 5 and 8 characters so that if it is less or more I have to print an error statement

13. [quote]NOTE: Your program must also validate user input. A valid user input is considered to be a positive integer. [/code]
This makes me think they will try to give your program bad data. Negative numbers, possibly non-numeric data too. I see nothing that guarantees non-numeric input.

However, if you are certain that the input will be numeric, then you can use your scanf method, and read in the input directly to an integer variable. That will make the prime check easy, and should take care of the leading zeros. This will "skip" leading zeros too. You can also check the number of digits by comparing against the smallest possible 5-digit number and the largest possible 8-digit number. For the palindrome check, the easiest way would be to use sprintf to print it to a char buffer/array, then use the method I suggested in post #3 above.

14. Do you have EPS II at uiowa? I have this same assignment and I can't figure it out how to even separate the variables to perform each check. Anduril462, how would write the scanf for the password? Would you take 8 separate variables within the scanf or just have one variable to store the entire password? After you use the scanf, how would you check the password for each step?

15. @me1000:
My previous posts in this thread give a few different suggestions for how I might approach this problem. If you read through it carefully, you would get some idea of where to begin. So re-read this, and start writing some code. If you run into trouble, start your own thread instead of hijacking jgkamin's, and you'll be sure to get some help.