isvowel assignment C# programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jmf777
    New Member
    • Aug 2008
    • 19

    isvowel assignment C# programming

    Hi,

    I am having serious trouble writing the code for an assignment I am working on. I am a C# beginner and any help would be much appreciated thank you.

    The question is:

    The ancient Egyptians wrote in hieroglyphics, in which vowel sounds are not represented, only consonants. Is written English generally understandable without vowels? To experiment, write a function isvowel ( ) that tests whether or not a character is a vowel. Use your function in a program that reads the standard input file and writes to the standard output file, deleting all vowels. Use redirection on a file containing some English text to test your program.

    what I have so far which is probably all wrong is:

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int isvowel(int ch);
    
    int main (void)
    {
    int c;
    
    while (isspace(c = getchar()) != EOF && c != '\n')
    	if (isvowel(c) == 1){
    		putchar(c);
    }
    
    return 0;
    }
    
    	int isvowel(int ch)
    	{
    		if (ch != 'A' || ch != 'a' || ch != 'E' || ch != 'e' || ch != 'I' ||
    			ch != 'i' || ch != 'O' || ch != 'o' || ch != 'U' || ch != 'u')
    			return 1;	
    		return 0;
    	}
    again any help is EXTREMELY appreciated thank you
    Last edited by kenobewan; Aug 30 '08, 01:16 PM. Reason: Use code tags
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Hi.
    That's not C#, just FYI. ;)

    Anyway, have you tried to run this code? If so, what happens?
    Looks like you've got the basic idea of how to solve the problem so please be specific about what isn't working and how we can help get it working.

    As a tip, you may want to use toupper() in your isvowel() routine so that you don't have to check both upper- and lower-case versions of each vowels (and you might wanna use a switch instead of a long condition in an if statement).

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      I think this condition while (isspace(c = getchar()) != EOF && c != '\n')
      is a issue.
      First check whether the character is not EOF and the check whether its a space.

      By the way what issue you are facing?

      Raghu

      Comment

      • jmf777
        New Member
        • Aug 2008
        • 19

        #4
        yes I have tried running the code. say if I put in asdfjkl it re-puts out asdfjkl and doesnt delete any vowels. I'm thinking their is something wrong with my isvowel function or something wrong with the main program and the way its using the returned 1 or 0 from my isvowel function.

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          Originally posted by jmf777
          yes I have tried running the code. say if I put in asdfjkl it re-puts out asdfjkl and doesnt delete any vowels. I'm thinking their is something wrong with my isvowel function or something wrong with the main program and the way its using the returned 1 or 0 from my isvowel function.
          Your isvowel is wrong, because condition x!='a' || x!='A' ... is always true. It looks like terms must be changed to ==. And is would be much easier to write a simple testcase like printf("%d", isvowel('x')); than wait for reply.
          And it's they're not their.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Originally posted by newb16
            And it's they're not their.
            No it's "there" not "their" and not "they're"

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Well, part of the problem is that you have backwards logic. Usually when you write a Boolean type function that asks a question, the answer should make sense. Your logic returns true when you pass a consonant to isvowel. To put it in context, it's like saying: it's true that 's' is a vowel. Doesn't make sense.

              Also, when you are doing not equals, you have to change your comparison logic a bit. Think of this:
              Code:
              if(c != 'a' || c != 'A')
              This will always be true, because 'a' != 'A' and will return true. What you need to do is flip that whole method's logic.
              Code:
              if(c == 'a' || c == 'A' ) return 1;
              And print the values that return 0.

              Comment

              • balabaster
                Recognized Expert Contributor
                • Mar 2007
                • 798

                #8
                Originally posted by insertAlias
                Well, part of the problem is that you have backwards logic. Usually when you write a Boolean type function that asks a question, the answer should make sense. Your logic returns true when you pass a consonant to isvowel. To put it in context, it's like saying: it's true that 's' is a vowel. Doesn't make sense.

                Also, when you are doing not equals, you have to change your comparison logic a bit. Think of this:
                Code:
                if(c != 'a' || c != 'A')
                This will always be true, because 'a' != 'A' and will return true. What you need to do is flip that whole method's logic.
                Code:
                if(c == 'a' || c == 'A' ) return 1;
                And print the values that return 0.
                I think the big problem is the combination of != and || - just at a glance. It should be:

                If Not A AND Not E AND Not I etc. etc.

                I personally would stick them all in a list of allowable characters and parse over the string... checking, it really depends on how long the string is going to be. Maybe I'd use regex, that'd be a nice clean elegant way of doing it without having to parse over character strings and lists of allowable chars.

                Code:
                Imports System.Text.RegularExpressions
                
                Module Module1
                
                  Function RemoveChars(ByVal SearchString As String, _
                                                 ByVal CharsToRemove As String) As String
                
                      Return Regex.Replace(SearchString, "(?i:["& CharsToRemove & "])", "")
                
                  End Function
                
                  Sub Main()
                  
                    Dim InvalidChars As String = "AEIOU"
                    Dim MyString As String = "The Quick Brown Fox Jumps Over The Lazy Dog"
                    MyString = RemoveChars(MyString, InvalidChars)
                
                  End Sub
                
                End Module
                And if you just needed to find out if a char was valid or not you'd return the IsMatch property of the Regex.

                Code:
                Function IsVowel(ByVal InputChar As Char) As Boolean
                
                  Return Regex.IsMatch(InputChar, "?i:[AEIOU]")
                
                End Function
                How I would go about doing that in C/C++, I can't remember, it's been a long time since I wrote C. Besides...then I'd be doing your homework for you rather than just giving pointers... in C# it's pretty easy, and the code isn't a world dissimilar to the VB code, so it should be a cinch to work it out...

                Comment

                • jmf777
                  New Member
                  • Aug 2008
                  • 19

                  #9
                  Hey thanks for the help! I got the program working by changing the != signs to == and editing the main program. I really appreciated the help and am going to re-read the chapter on relational, equality, and logical operators. Thanks again

                  Comment

                  • balabaster
                    Recognized Expert Contributor
                    • Mar 2007
                    • 798

                    #10
                    Originally posted by jmf777
                    Hey thanks for the help! I got the program working by changing the != signs to == and editing the main program. I really appreciated the help and am going to re-read the chapter on relational, equality, and logical operators. Thanks again
                    Yeah, no problem. If there's anything you're not getting, just yell.

                    Comment

                    Working...