int getPhoneNumber(char * userEntry)
{
// allow 3 formats, spaces allowed
// format 1: (555) 123 - 4567
// format 2: 555 -123 -4567
// format 3: 123 - 4567
// first strip out spaces, compressing string to have no spaces
// 555 - 123 -4567 will become 555-123-4567
char * tempPtr;
char * spaceRemovalPtr;
char shiftChar;
tempPtr = userEntry;
gets(userEntry);
while (*tempPtr != NULL) {
printf("char: %c\n",*tempPtr);
// see if the character is a space
if (*tempPtr == ' ') {
printf("You found a space\n");
// now we need to shift everything remaining left one space
spaceRemovalPtr = tempPtr;
while (*spaceRemovalPtr != NULL) {
shiftChar = *(spaceRemovalPtr + 1);
*spaceRemovalPtr = shiftChar;
spaceRemovalPtr++;
}
printf("Adjusted string: %s\n",userEntry);
}
else {
tempPtr++;
}
}
printf("final string: %s\n",userEntry);
// at this point, we have removed all spaces
// allow 3 formats
// format 1: (555)123-4567
// format 2: 555-123-4567
// format 3: 123-4567
// if format 1 fits, return 1
if (
(userEntry[0] == '(') &&
(userEntry[1] >= '0' && userEntry[1]<='9') &&
(userEntry[2] >= '0' && userEntry[2]<='9') &&
(userEntry[3] >= '0' && userEntry[3]<='9') &&
(userEntry[4] ==')') &&
(userEntry[5] >= '0' && userEntry[5]<='9') &&
(userEntry[6] >= '0' && userEntry[6]<='9') &&
(userEntry[7] >= '0' && userEntry[7]<='9') &&
(userEntry[8] == '-') &&
(userEntry[9] >= '0' && userEntry[9]<='9') &&
(userEntry[10] >= '0' && userEntry[10]<='9') &&
(userEntry[11] >= '0' && userEntry[11]<='9') &&
(userEntry[12] >= '0' && userEntry[12]<='9'))
{
return 1;
}
// if format 2 fits, return 1
// 123-456-7890
if (
(userEntry[0] >= '0' && userEntry[0]<='9') &&
(userEntry[1] >= '0' && userEntry[1]<='9') &&
(userEntry[2] >= '0' && userEntry[2]<='9') &&
(userEntry[3] == '-') &&
(userEntry[4] >= '0' && userEntry[4]<='9') &&
(userEntry[5] >= '0' && userEntry[5]<='9') &&
(userEntry[6] >= '0' && userEntry[6]<='9') &&
(userEntry[7] == '-') &&
(userEntry[8] >= '0' && userEntry[8]<='9') &&
(userEntry[9] >= '0' && userEntry[9]<='9') &&
(userEntry[10] >= '0' && userEntry[10]<='9') &&
(userEntry[11] >= '0' && userEntry[11]<='9'))
{
return 1;
}
// if format 3 fits, return 1
if (
(userEntry[0] >= '0' && userEntry[0]<='9') &&
(userEntry[1] >= '0' && userEntry[1]<='9') &&
(userEntry[2] >= '0' && userEntry[2]<='9') &&
(userEntry[3] == '-') &&
(userEntry[4] >= '0' && userEntry[4]<='9') &&
(userEntry[5] >= '0' && userEntry[5]<='9') &&
(userEntry[6] >= '0' && userEntry[6]<='9') &&
(userEntry[7] >= '0' && userEntry[7]<='9'))
{
return 1;
}
// nothing fit, return 0 (bad entry)
return 0;
}