0

I have a problem where I have to print a string read from the console after the following rule: After two successive vowels or consonants I need to print the character _ but not if there are two vowels or two consonants at the end of the string, then I have to print the number of the substrings separated by each _ character. The string is read until EOF.

I have run into two problems:

  • The program will not terminate unless I type EOF (Ctrl-Z) on a new line. If I just add it at the end of the string, it will continue.

  • I need a way to check if the last character printed is '_', and to remove it if it is at the end of the printed string.

Here's my code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main()
{
    char vow[]="aeiouAEIOU", c,x;
    int nr=1;
    x=getchar();
    putchar(x);
    while((c=getchar())!=EOF)
    {
        if(isalpha(c))
        {
            if (((strchr(vow,x)&&strchr(vow,c))||(!strchr(vow,x)&&!strchr(vow,c)))&&x!=0)
            {
                putchar(c);
                putchar('_');
                nr++;
                x=0;
            }
            else
            {
                putchar(c);
                x=c;
            }
        }
    }
    printf("\n%d",--nr);
    return 0;
}
10
  • 2
    Can you provide an example input/output text of the desired result? Commented Jan 26, 2017 at 11:50
  • The Ctrl-Z is addressed at : stackoverflow.com/questions/19372774/… Commented Jan 26, 2017 at 11:50
  • 3
    while((c=getchar())!=EOF) c should be an int, not a char. Commented Jan 26, 2017 at 12:02
  • 1
    There is your code indeed. What's wrong with it? What is the question? Commented Jan 26, 2017 at 12:04
  • 1
    As a side note, you are calling strchr more often than necessary. If you want to write an a XOR b in C, you can use !a != !b (!strchr(vow,x) != !strchr(vow,c), additionally negate the result for your case, so its actually !strchr(vow,x) == !strchr(vow,c)). See stackoverflow.com/questions/1596668/logical-xor-operator-in-c Commented Jan 27, 2017 at 6:36

1 Answer 1

2

The program will not terminate unless I type EOF "^Z" on a new line. If I just add it at the end of the string, it will continue.

This is a windows command prompt issue. The Ctrl+z is not really EOF, it is just a signal for the command prompt that is interpreted as EOF when it appears solo in a line. If you feed the program stdin from terminal input and not from a file, reaching EOF is not generally available.

I suggest you live with this issue and create some sample.txt file as input. Then you can redirect stdin to read from this file and it will encounter EOF whenever the whole file was read.

I need a way to check if the last character printed is '_', and to remove it if it is at the end of the printed string.

Don't.

Instead of printing and removing, you could keep some boolean value that indicates whether a _ should be printed before the next alphanumeric character. But you only ever print it, if there actually is a next character to be printed.

int printSeparator = 0;
// ...

    if(isalpha(c))
    {
        if (printSeparator)
        {
            putchar('_');
            printSeparator = 0;
        }
        if (((strchr(vow,x)&&strchr(vow,c))||(!strchr(vow,x)&&!strchr(vow,c)))&&x!=0)
        {
            putchar(c);
            //putchar('_');
            printSeparator = 1;
            nr++;
            x=0;
        }
        else
        {
            putchar(c);
            x=c;
        }
    }

You may want to move the nr++ counter to the same place where the _ character is printed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.