I am trying to write a program that reads a string (in my case a domain name). What I am struggling with is how to traverse through the array and see if there are any invalid characters in the string such as a semi colon or a space. Here is what I have as of now.
#include <stdio.h>
int main()
{
char min[1];
char max[253];
char domain[253];
printf("Enter a domain: ");
scanf("%s", domain);
return 0;
}
#include <stdio.h> /** * Given a character this function returns 0 if it's not valid and 1 if it's valid */ int is_valid(char ch) { return !(ch == ';' || ch == ' '); // you can add more invalid characters here.. } /** * Given a domain this function returns 0 if it's not valid and 1 if it's valid */ int is_domain_valid(char domain[]) { int i = 0; while (domain[i]) { if (!is_valid(domain[i])) { return 0; } i++; } return 1; } int main() { char min[1]; char max[253]; char domain[253]; printf("Enter a domain: "); scanf("%s", domain); if (is_domain_valid(domain)) { printf("%s is a valid domain\n", domain); } return 0; }
Get Answers For Free
Most questions answered within 1 hours.