Solved

Removing printf function

Posted on 2000-04-18
4
313 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
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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 opening and writing to files in the C programming language.

777 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