Question

C programming question: Write a function char isValid (char c), return a with a char value...

C programming question:

Write a function char isValid (char c), return a with a char value of 1 if it is a valid char, and return 0 if it is invalid. (DO NOT USE POINTER!)

valid char are '1' '2' 3' '4' '5' 'A' 'B' 'C'

for example:

lower case is not valid

'1' is valid

'A' is valid

'B' is valid

'ABC' is not valid

'D' is not valid

'9' is not valid

'1234' is not valid

For Testing: make a user input and then put into the function, the program should have loop if the user does not input valid char. (write it into main function!)

Homework Answers

Answer #1
#include <stdio.h>

char isValid (char c){
    if(c=='1' || c=='2' || c=='3' || c=='4' || c=='5' || c=='A' || c=='B' || c=='C'){
        return '1';
    }
    else{
        return '0';
    }
}

int main(){
    char c, tmp;
    
    printf("Enter valid char: ");
    scanf("%c",&c);
    scanf("%c",&tmp);
    
    while(isValid(c)=='0'){
        printf("%c is not a valid character\n",c);
        printf("Please enter valid char: ");
        scanf("%c",&c);
        scanf("%c",&tmp);
    }
    
    printf("%c is a valid character\n",c);
    
    return 0;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a program in C++ coding that asks the user to input an integer between 25...
Write a program in C++ coding that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2...
C programming Write a function that takes in a 2D char array (string array) and return...
C programming Write a function that takes in a 2D char array (string array) and return a 1D char array with all the elements connected together Hint: strlen(-char*-) //returns the length of a string strcat(-char* accum-, something to add) //works like string+= in java
(Longest common prefix, C-string, loop, char comparison) Write the prefix function to find the longest prefix...
(Longest common prefix, C-string, loop, char comparison) Write the prefix function to find the longest prefix of two strings using C-strings with the following header: void prefix( const char s1[ ], const char s2[ ], char commonPrefix[ ]) Write a test program that prompts the user to enter two C-strings and displays their common prefix. Sample run :- String 1: Programming is fun String 2: Program logic The common prefix is program. ---- Can you please explain the code too...
(c programming) Given the function declaration and documentation below, write the function definition. Do not use...
(c programming) Given the function declaration and documentation below, write the function definition. Do not use the standard library function fgetc. /// @brief Reads a character from specified file input stream. /// @param instream A FILE* variable for an input stream to read a character from. /// @return A character of type char from instream. /// @note This function assumes #include <stdio.h> and a working, valid FILE* variable to an input stream. char grabchar(FILE* instream);
Write a program that asks the user to input an integer between 25 and 50, inclusive....
Write a program that asks the user to input an integer between 25 and 50, inclusive. Utilize a WHILE loop to test for INVALID input. If the input is INVALID, the loop will repeat, and ask the user to try again. The pseudocode looks like this: Prompt user to input an integer between 25 and 50 Accept the input from the user Test for invalid input (HINT: use a while loop and the OR operator with 2 relational expressions. You...
A. Write C code to create a structure called time_of_day, which stores the current time in...
A. Write C code to create a structure called time_of_day, which stores the current time in hours, minutes, and seconds. All the fields should be integers except for seconds, which should be a floating point value. B. Write a C function called check_time, which takes a pointer to a time_of_day structure as input, and return 1 if the time is valid (0 to 23 hours, 0 to 59 minutes, 0 to 59.999999 seconds) and 0 otherwise. Assume that times are...
Programming in C language (not C++) Write a main function with a function call to a...
Programming in C language (not C++) Write a main function with a function call to a function called Calculation which has two integer arguments/ parameters. The function returns a character. Declare and initialize the necessary data variables and assign balues needed to make it executable and to prevent loss of information. //input 2 integer values //returns the characterequivalent of the higher of the two integer values char Calculation(int, int);
write a Program that converts uppercase character to lower case letter character. Declare function char toLower(...
write a Program that converts uppercase character to lower case letter character. Declare function char toLower( char ch); to do the conversion.if ch is not an uppercase character the function should return ch unchanged. if it is an uppercase character ,add the difference of 'a' and 'A' to ch as the return value. Write your program with a global variable for the actual parameter. Translate to Pep/9 assembly language. Pep/9 assembly language Only please
Part 1 Write a program that reads a line of input and display the characters between...
Part 1 Write a program that reads a line of input and display the characters between the first two '*' characters. If no two '*' occur, the program should display a message about not finding two * characters. For example, if the user enters: 1abc*D2Efg_#!*345Higkl*mn+op*qr the program should display the following: D2Efg_#! 1) Name your program stars.c. 2) Assume input is no more than 1000 characters. 3) String library functions are NOT allowed in this program. 4) To read a...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...