lhutton
asked on
Removing printf function
I'm trying to take out the line that reads "Must enter at least one character!" from the following program of mine but I can't work out how to do it right. I thought could just take out the whole if function:
if ( (len = noSpace(aCopy)) == 0 )
{
printf("Must enter at least one character!\n");
return 0;
}
What am I missing???
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 128
int noSpace(char* s)
{
int l = 0;
while ( *s != 0 )
{
if ( !isalpha(*s) )
{
memmove(s, s+1, strlen(s+1) + 1);
continue;
}
*s = tolower(*s);
s++;
l++;
}
return l;
}
int main()
{
char line[MAX];
char aCopy[MAX];
char *ptr1, *ptr2;
int len;
printf("Test for palindromes\n============= =======\n" );
while ( 1 )
{
printf("\nEnter string: ");
gets(line);
strcpy(aCopy, line);
if ( (len = noSpace(aCopy)) == 0 )
{
printf("Must enter at least one character!\n");
return 0;
}
for (ptr1 = aCopy, ptr2 = aCopy + len - 1; ptr1 < ptr2; ptr1++, ptr2--)
if ( *(ptr1) != *(ptr2) )
{
printf("That isn't a palindrome.\n");
break;
}
if ( ptr1 >= ptr2 )
printf("That is a palindrome.\n");
printf("\nAnother? (y|n) ");
gets(aCopy);
if ( tolower(aCopy[0]) == 'n' )
break;
}
printf("\nGoodbye\n");
return 0;
}
if ( (len = noSpace(aCopy)) == 0 )
{
printf("Must enter at least one character!\n");
return 0;
}
What am I missing???
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 128
int noSpace(char* s)
{
int l = 0;
while ( *s != 0 )
{
if ( !isalpha(*s) )
{
memmove(s, s+1, strlen(s+1) + 1);
continue;
}
*s = tolower(*s);
s++;
l++;
}
return l;
}
int main()
{
char line[MAX];
char aCopy[MAX];
char *ptr1, *ptr2;
int len;
printf("Test for palindromes\n=============
while ( 1 )
{
printf("\nEnter string: ");
gets(line);
strcpy(aCopy, line);
if ( (len = noSpace(aCopy)) == 0 )
{
printf("Must enter at least one character!\n");
return 0;
}
for (ptr1 = aCopy, ptr2 = aCopy + len - 1; ptr1 < ptr2; ptr1++, ptr2--)
if ( *(ptr1) != *(ptr2) )
{
printf("That isn't a palindrome.\n");
break;
}
if ( ptr1 >= ptr2 )
printf("That is a palindrome.\n");
printf("\nAnother? (y|n) ");
gets(aCopy);
if ( tolower(aCopy[0]) == 'n' )
break;
}
printf("\nGoodbye\n");
return 0;
}
If you don't want the "Must enter..." message, just don't print it and do a 'break' to kick out of the while(1) - like you do elsewhere.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks :)
ASKER