Thread: Question for Alternative Solution in Program

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    93

    Question for Alternative Solution in Program

    In this crazy vowel remover program I made:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    char isvowel (char a);
    
    int main () {
    
    	char c;
    
    	printf("Enter whatever you want to be stripped of vowels. Press # when done.\n\n");
    
    	while ((c = getchar())!= '#'){
    	c = isvowel(c);
    
    	if (c == '\n'){
    	    putchar('\n');
    	}
    
    	putchar(c);
    
    	}
    
    	return 0;
    
    }
    
    char isvowel (char a) {
    
    	switch (a) {
    
    	case 'A': 
    	case 'a': a='\a';
    			break;
    
    	case 'E': 
    	case 'e': a='\a';
    			break;
    
    	case 'I': 
    	case 'i': a='\a';
    			break;
    
    	case 'O':
    	case 'o': a='\a';
    			break;
    
    	case 'U': 
    	case 'u': a='\a';
    			break;
    
    	case 'Y': 
    	case 'y': a='\a';
    			break;
    
    	}
    
    	return a;
    
    }
    Are there any alternate ways to make vowels a blank space?

    I'm not able to use printf(""); because the function has to return a char, and a = ''; doesn't work.

    I used the \a for alarm, because it never really did crap for me.

    By the way, if anyone can tell me any uses for \a, I'd appreciate it..since everywhere I go it just tells me it's an alarm.

    I'm working on a word counter program, which I will post in a bit when I fix it up.

    I don't want to use arrays or pointers with these because not only do I not understand them very well yet, but I don't want to move to more advanced stuff without knowing the earlier stuff down pat.

    Thanks.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Ah, and I've been racking my brain to put a \n inbetween the user's input and my output.

    Can't think of a way to do it with the current structure because it's in a while loop.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int isvowel( int c )
    {
        switch( c )
        {
            /* A E I O U... */
            case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U':
                return 1;
            /* and sometimes Y... ;) */
            case 'y': case 'Y': return rand()%10 == 0;
        }
        return 0;
    }
    
    ...
    
    while( (c = getchar( )) != '#' )
    {
        putchar( isvowel( c ) ? ' ' : c );
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Are there any alternate ways to make vowels a blank space?
    Yes, there are countless ways. Here's another:
    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    /* isvowel is a reserved name */
    int is_vowel(int c);
    
    int
    main(void)
    {
      char   buffer[BUFSIZ];
      char   ch;
      size_t i = 0;
    
      /* Read a string from standard input */
      while ((ch = getchar()) != EOF && ch != '\n') {
        buffer[i++] = ch;
        if (i == sizeof buffer - 1) {
          break;
        }
      }
      buffer[i] = '\0';
      printf("|%s|\n", buffer);
      /* Replace vowels  with spaces */
      for (i = 0; buffer[i] != '\0'; i++) {
        if (is_vowel(buffer[i])) {
          buffer[i] = ' ';
        }
      }
      printf("|%s|\n", buffer);
    
      return 0;
    }
    
    int
    is_vowel(
      int c
      )
    {
      c = toupper(c);
    
      return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
    }
    My best code is written with the delete key.

  5. #5
    Quote Originally Posted by quzah
    Code:
    int isvowel( int c )
    Warning. Identifiers beginning with 'is' followed by a lowercase are reserved for the language extensions. Details in
    WG14/N869 Committee Draft — January 18, 1999 413
    7.26 Future library directions
    Code:
    int is_vowel( int c )
    is fine.
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quzah, that is an awesome way to put a switch statement if you want to save space. I also like the way it works by working with return values. Good template to do in other programs.

    Prelude, that is seems like a great way to do it as well, but I am about to learn arrays right now, so I have no idea how to use that yet lol. I will check it out though once I do.

    However, I already knew how to do the ' ' for inserting a blank space...I was just wondering how would you send back no space? Ie. remove the vowel and insert nothing. Only way I was able to do it was using \a.

    Also, are there actually any uses for \a? It says its an alarm, an alarm for what? It's not going to wake me up in the morning or anything.

    Thanks guys.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    \a is defined in ansi standard somewhere(I don't have a copy) to alert the user without the use of buffers. Basically it can just be an alert(That is what I call it) to get your attention.

    to quzah
    Code:
    return rand()%10 == 0;
    rand returns an int between 0 and randmax. Are you just joking here? Does it mean if say returned 101 then it would return 0 right? and for 100 1.
    Last edited by linuxdude; 06-21-2004 at 07:08 PM.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I was just wondering how would you send back no space?
    Something like this?
    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int is_vowel(int c);
    
    int
    main(void)
    {
      int c;
    
      while ((c = getchar()) != '#') {
        if (!is_vowel(c)) {
          putchar(c);
        }
      }
    
      return 0;
    }
    
    int
    is_vowel(
      int c
      )
    {
      c = toupper(c);
    
      return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
    }
    >Also, are there actually any uses for \a?
    It's a standard way to create an audible or visible alert. You can use it however you like. I've never had cause to use it though, other than with fun test programs.
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by Prelude
    >I was just wondering how would you send back no space?
    Something like this?
    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int is_vowel(int c);
    
    int
    main(void)
    {
      int c;
    
      while ((c = getchar()) != '#') {
        if (!is_vowel(c)) {
          putchar(c);
        }
      }
    
      return 0;
    }
    
    int
    is_vowel(
      int c
      )
    {
      c = toupper(c);
    
      return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
    }
    >Also, are there actually any uses for \a?
    It's a standard way to create an audible or visible alert. You can use it however you like. I've never had cause to use it though, other than with fun test programs.
    Yep, that code does it.

    So, what exactly is going on in there...if is_vowel does not return a true value, you print out c.

    So in the is_vowel function, the

    Code:
    return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U';
    basically means that you return c if it equals a vowel. So it should only return true, when it's a vowel causing only consonsants to be printed.

    So returns are allowed to have boolean expressions? It can function like an if statement?

    The return part only functions, or returns a true value when it's a vowel, so the return part acts as an if statement?

    Sorry for the confusing wording, but I'm sure you can get the gist of what I'm asking.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >basically means that you return c if it equals a vowel.
    No, it returns the result of a boolean expression, 0 or 1. If any of the tests fail then is_vowel returns 0, otherwise it returns 1. That value can further be used by the calling function to do something special with vowels if is_vowel returns 1 (in this case, not writing the character).

    >So returns are allowed to have boolean expressions?
    Yes.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    93
    Quote Originally Posted by Prelude
    >basically means that you return c if it equals a vowel.
    No, it returns the result of a boolean expression, 0 or 1. If any of the tests fail then is_vowel returns 0, otherwise it returns 1. That value can further be used by the calling function to do something special with vowels if is_vowel returns 1 (in this case, not writing the character).

    >So returns are allowed to have boolean expressions?
    Yes.
    Ahhhh, now I understand.

    I forgot the == was also a comparison to check if something is equal or not, and returns a value.

    I am no longer confuzzled now.

  12. #12
    Registered User
    Join Date
    May 2004
    Posts
    6
    I'm not quite sure what the following code posted by quzah means:
    rand()%10 == 0
    I understand 'rand()%10' is generating a random number between 0 and 10, easy. But the section of code that reads '== 0', I have no idea what it does or means, would somebody mind explaining it?

  13. #13
    Quote Originally Posted by errno
    I'm not quite sure what the following code posted by quzah means:
    I understand 'rand()%10' is generating a random number between 0 and 10, easy.
    Of course, you meant 9, not 10...
    But the section of code that reads '== 0', I have no idea what it does or means, would somebody mind explaining it?
    This expression, like any logical expression, returns 0 or 1.

    The possible values for rand() % 10 are 0 to 9.

    The value of the expression is:

    Code:
    |rand() % 10 | expression|
    |------------|-----------|
    |    0       |    1      |
    |    1       |    0      |
    |    2       |    0      |
    |    3       |    0      |
    |    4       |    0      |
    |    5       |    0      |
    |    6       |    0      |
    |    7       |    0      |
    |    8       |    0      |
    |    9       |    0      |
    Emmanuel Delahaye

    "C is a sharp tool"

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'm not quite sure what the following code posted by quzah means:
    It means it has a 10% chance of being true
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem
    > I'm not quite sure what the following code posted by quzah means:
    It means it has a 10% chance of being true
    That's what you all get for trying to take a joke literally. I guess they didn't catch it. (I hate having to explain jokes.) List your vowels:

    A, E, I, O, U, and sometimes Y...

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  2. Password Program Question
    By SirTalkAlot415 in forum C++ Programming
    Replies: 13
    Last Post: 11-06-2007, 12:35 PM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Replies: 8
    Last Post: 03-26-2002, 07:55 AM