• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

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;
}
0
lhutton
Asked:
lhutton
  • 2
1 Solution
 
lhuttonAuthor Commented:
I just want it to jump straight to "Goodbye" if nothing is entered.
0
 
cookreCommented:
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.
0
 
Jan LouwerensCommented:
Here is a code example of what has been suggested.

Replace the following:

if ( (len = noSpace(aCopy)) == 0 )
{
   printf("Must enter at least one character!\n");
   return 0;
}


.... with this:

if ( (len = noSpace(aCopy)) == 0 )
   break;
0
 
lhuttonAuthor Commented:
Thanks :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now