After compiling with:
gcc Lab10.c -Wall -Werror
./a.out (636)394-6659
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
/*
Parameters
token - A string
Return:
1 if the string is a valid phone number,
0 otherwise
A valid phone number takes the form
(xxx)xxx-xxxx where x is a digit 0-9
*/
int isPhoneNumber(char* token);
int main(int argc, char* argv[])
{
//This is a valid phone number
assert(isPhoneNumber("(123)456-7890") == 1);
//These are not valid phone numbers
assert(isPhoneNumber("123-456-7890") == 0);
assert(isPhoneNumber("1234567890") == 0);
assert(isPhoneNumber("(123) 456-7890") == 0);
assert(isPhoneNumber("Hello World") == 0);
assert(isPhoneNumber("") == 0);
printf("Congratulations! You finished the prelab!");
return 0;
}
int isPhoneNumber(char* token)
{
int i, digit;
char leftBrack [2], rightBrack [2], dash[2];
strcpy(leftBrack, "(");
strcpy(rightBrack, ")");
strcpy(dash, "-");
for (i=1;i<14;i++)
{
if (i == 1 &&(strcmp(token, leftBrack) != 0))
return 0;
if (i > 1 || i < 5)
{
digit = atoi(token);
if (digit < 0 || digit > 9)
return 0;
}
if (i == 5 && (strcmp(token,rightBrack) != 0))
return 0;
if (i > 5 || i <9)
{
digit = atoi(token);
if (digit < 0 || digit > 9)
return 0;
}
if (i == 9 && (strcmp(token, dash) != 0))
return 0;
if (i > 9 || i < 14)
{
digit = atoi(token);
if (digit < 0 || digit > 9)
return 0;
}
}
return 1;
}
Execute this way:
./a.out "(636)394-6659"
Or as you don't check arguments, this way is the same:
./a.out
You don't use argc
or argv
, so use this prototype for main()
int main()
Also, you need to consider string a null terminated arrays of chars.
char leftBrack [2], rightBrack [2], dash[2];
strcmp()
does not do what you believe it does. You need to use strncmp
with a third argument equal to 1. Also atoi()
checks the whole string, not a mere character, just check token[i] - '0'
is a number between 0 and 9.
Fixed code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
/*
Parameters
token - A string
Return:
1 if the string is a valid phone number,
0 otherwise
A valid phone number takes the form
(xxx)xxx-xxxx where x is a digit 0-9
*/
int isPhoneNumber(char* token);
int main()
{
//This is a valid phone number
assert(isPhoneNumber("(123)456-7890") == 1);
//These are not valid phone numbers
assert(isPhoneNumber("123-456-7890") == 0);
assert(isPhoneNumber("1234567890") == 0);
assert(isPhoneNumber("(123) 456-7890") == 0);
assert(isPhoneNumber("Hello World") == 0);
assert(isPhoneNumber("") == 0);
printf("Congratulations! You finished the prelab!");
return 0;
}
int isPhoneNumber(char* token)
{
int i, digit;
char leftBrack [2], rightBrack [2], dash[2];
strcpy(leftBrack, "(");
strcpy(rightBrack, ")");
strcpy(dash, "-");
for (i=0;i<13;i++)
{
switch (i)
{
case 0:
if (strncmp(token + i, leftBrack, 1) != 0)
return 0;
break;
case 4:
if (strncmp(token + i, rightBrack, 1) != 0)
return 0;
break;
case 8:
if (strncmp(token + i, dash, 1) != 0)
return 0;
break;
default:
digit = token[i] - '0';
if (digit < 0 || digit > 9)
return 0;
}
}
return 1;
}