Solved

Removing printf function

Posted on 2000-04-18
4
311 Views
Last Modified: 2010-04-15
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
Comment
Question by:lhutton
  • 2
4 Comments
 

Author Comment

by:lhutton
Comment Utility
I just want it to jump straight to "Goodbye" if nothing is entered.
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
 
LVL 5

Accepted Solution

by:
Jan Louwerens earned 29 total points
Comment Utility
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
 

Author Comment

by:lhutton
Comment Utility
Thanks :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now