***MUST BE DONE IN C PROGRAMMING LANGUAGE*** You are creating a new account on a
ID: 3885691 • Letter: #
Question
***MUST BE DONE IN C PROGRAMMING LANGUAGE***
You are creating a new account on a website and need to come up with a username. The username has the following requirement:
1) The username has at least 5 characters, and at most 10 characters
2) It must contain alphabetic letters (upper or lower case), digit or underscore, but no space or other punctuations.
For example, mike_FL is valid and mike!FL is invalid
Write a program that takes a word entered by the user, check if the word is a valid username. If the input is valid, print a confirmation statement. If it is not, print a statement that the input is not valid.
1) Name your program username.c.
2) The user input ends with the user pressing the enter key (a new line character).
3) Use getchar() to read in the input.
4) You can use character handling functions such as isdigit and isalpha. Don’t forget to include ctype.h if you use any character handling functions
Explanation / Answer
#include<stdio.h>
#include<ctype.h>
int main() {
int valid = 1, len = 0;
char ch;
do {
ch = getchar();
if(!isdigit(ch) && !isalpha(ch) && ch!='_' && ch!= ' ')
valid = 0;
len++;
}while(ch != ' ');
if (len < 5 || len > 10)
valid = 0;
printf("password is %s ",valid?"valid":"not valid"); //notice the use of tarnary operator
}
Let me know if you like the code!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.