Solved

Removing printf function

Posted on 2000-04-18
4
316 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

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

Expert Comment

by:cookre
ID: 2728961
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
ID: 2729026
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
ID: 2729214
Thanks :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to save 10 174
xamarin c# deserialize Json containing nested object 2 150
What is sub-make ? 2 88
A  &&  B  || (C||D) how it works in c 7 153
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

730 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