help with program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bobcats
    New Member
    • Jan 2007
    • 13

    help with program

    hi guys. im a newbie with regards to C. we were given a code that takes an input, removes vowels and leaves only a space from the input and prints the output. here's the code:
    Code:
    #include<ctype.h>
    #include<stdio.h>
    
    void remove_vowel(char x;char y; char z; char v; char u)
    {
    int c, last_c = '\O';
    int d;
    
    d = 1;
    while((c=getchar()) != '\n')
    { if (c == '\n'){
    	if (last_c != '\n')
     	      putchar(c)
    }
    else if (c !=x && c !=y && c !=z && c !=u && c !=x && c !='')
    	putchar(c);
    else if (c == '' && d == 1)
    {  ++d;
       putchar(c);
       last_c =c;
    }
    }
    
    printf("\n")
    }
    
    int main(void)
    {
    char a,e,i,o,u;
    
    remove_vowel(char 'a';char 'e';char 'i';char 'o';char 'u';)
    return(0);
    }
    I have a few questions about this code.
    1)what does last_c and '\O' stand for? what do they do?
    2) whats also the use of '\n' in the getchar?
    3)what does d stand for?
    4)Is there anyway to emulate the program with the use of only basic functions and loops?

    Wish that someone can explain the logic of the code for me. I'm really having a hard time with C. Thanks to all that would help.
    Last edited by horace1; Jan 29 '07, 12:19 PM. Reason: added code tags
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Code:
    #include<ctype.h>
    #include<stdio.h>
    
    void remove_vowel(char x;char y; char z; char v; char u)
    {
       int c, last_c = '\0';
       int d;
    
       d = 1;
       while((c=getchar()) != '\n')
       {
          if (c == '\n')
          {
             if (last_c != '\n') putchar(c)
          }
          else if (c !=x && c !=y && c !=z && c !=u && c !=x && c !='') putchar(c);
          else if (c == '' && d == 1)
          {
             ++d;
             putchar(c);
             last_c =c;
          }
       }
    
       printf("\n")
    }
    
    int main(void)
    {
       char a,e,i,o,u;
    
       remove_vowel(char 'a';char 'e';char 'i';char 'o';char 'u';)
       return(0);
    }
    Originally posted by bobcats
    1)what does last_c and '\0' stand for? what do they do?
    last_c appears to be a variable keeping track of the last character entered. It is initialized to '\0', which is the escape sequence for the null character. This would make sense, since at the start of the function, there is no 'previous' character.

    Originally posted by bobcats
    2) whats also the use of '\n' in the getchar?
    '\n' is the newline character. When output, it will move the printing position from the current line to the next line. It is coupled with the getchar() statement because the function does not want to continue if a newline character is entered. A newline character is entered whenever the enter key is pressed during input.

    Originally posted by bobcats
    3)what does d stand for?
    d appears to be a variable indicating whether or not you have already printed a space. If there has been a space printed, d is set to 2, and so the third else if branch cannot be executed again.

    Originally posted by bobcats
    4)Is there anyway to emulate the program with the use of only basic functions and loops?
    Why don't you try?

    Comment

    • bobcats
      New Member
      • Jan 2007
      • 13

      #3
      thanks for the help sir Ganon11. You answers have helped me a lot. I still have some followup questions regarding the code
      1. Why was the function of type void? Can't it be char because it would return char?
      2. Why is the printf in the function only printf("/n")
      Thanks again for the answers and hope that you could help me again with this one.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Code:
        #include<ctype.h>
        #include<stdio.h>
        
        void remove_vowel(char x;char y; char z; char v; char u)
        {
           int c, last_c = '\0';
           int d;
        
           d = 1;
           while((c=getchar()) != '\n')
           {
              if (c == '\n')
              {
                 if (last_c != '\n') putchar(c)
              }
              else if (c !=x && c !=y && c !=z && c !=u && c !=x && c !='') putchar(c);
              else if (c == '' && d == 1)
              {
                 ++d;
                 putchar(c);
                 last_c =c;
              }
           }
        
           printf("\n")
        }
        
        int main(void)
        {
           char a,e,i,o,u;
        
           remove_vowel(char 'a';char 'e';char 'i';char 'o';char 'u';)
           return(0);
        }
        Originally posted by bobcats
        1. Why was the function of type void? Can't it be char because it would return char?
        The function could return a char array, or a string. In this way, you could use that string later in your program if need be. As it is right now, however, the only purpose of the function is to print a string without any vowels in it, so there is no return type required. If you needed to, you could easily edit this function to return a pointer to a character array.

        Originally posted by bobcats
        2. Why is the printf in the function only printf("/n")
        At this point in the function, the putchar(c) function calls have already output the entire message, so there is nothing further to be output. Thus, for formatting purposes (so that there will be a blank line bewteen the end of your code and the next line in the Command Prompt), you output a newline character.

        Comment

        • bobcats
          New Member
          • Jan 2007
          • 13

          #5
          Thanks again sir. I deeply appreciate your help. I hope you wouldn't mind if I would ask another question.
          1)why was the expression 'last_c = c' used in the part of the if function regarding the use of space? Can't it be 'last_c = putchar(c)'?
          2) Am i wrong if i say that the getchar() in the code is for storing the input and the putchar() is for printing the output if it met the criteria?
          Thanks again sir for the help. S

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Originally posted by bobcats
            1)why was the expression 'last_c = c' used in the part of the if function regarding the use of space? Can't it be 'last_c = putchar(c)'?
            I'm not sure about the specifics, but I don't think putchar() is a value returning function, but rather a void function that outputs a single character. Thus, last_c = putchar(c) would not be syntactically correct.

            Originally posted by bobcats
            2) Am i wrong if i say that the getchar() in the code is for storing the input and the putchar() is for printing the output if it met the criteria?
            That's correct.

            No problem - glad to help someone understand some concepts.

            Comment

            • bobcats
              New Member
              • Jan 2007
              • 13

              #7
              Thanks again sir for all your help. Without your explanations I would be very lost regarding this code. I really appreciate it. Thanks again sir.

              Comment

              • bobcats
                New Member
                • Jan 2007
                • 13

                #8
                Um I made some minor alterations to the code above. I changed some variable names but essentially its still the same. But when I run the program it only prints several newlines after the input. Another problem is that the '\O' escape sequence was said to be invalid by my compiler. I dont know whats wrong. I hope someone could help me with this.

                Code:
                #include<ctype.h>
                #include<stdio.h>
                
                void erase(char ltr1, char ltr2, char ltr3, char ltr4, char ltr5)
                
                {
                        int text;
                        int spaces;
                        int previous_text;
                
                        spaces = 0;
                        previous text = '\O';
                        while( (text=getchar() ) != '\n')
                        { if (text == '\n')
                                {
                                if (previous_text != '\n')
                                        putchar(text);
                                else if (text!=ltr1 && text!=ltr2 && text!=ltr3 && text!=ltr4 && text!=ltr5 && text!= ' ')
                                                putchar(text);
                                else if (text==' ' && spaces==0)
                                                  ++spaces;
                                                   putchar(text);
                                                   previous_text = text;
                
                                }
                
                        printf("\n");
                        }
                }
                
                int main(void)
                {
                        char a;
                        char e;
                        char i;
                        char o;
                        char u;
                
                        erase(a,i,e,o,u);
                        return(0);
                }

                Comment

                • Ganon11
                  Recognized Expert Specialist
                  • Oct 2006
                  • 3651

                  #9
                  Code:
                  #include<ctype.h>
                  #include<stdio.h>
                  
                  void erase(char ltr1, char ltr2, char ltr3, char ltr4, char ltr5)
                  
                  {
                          char text;
                          int spaces;
                          char previous_text;
                  
                          spaces = 0;
                          previous_text = '\0'; // added _; also, \O is wrong: correct is \0 with a zero
                          while( (text=getchar() ) != '\n')
                          { if (text == '\n')
                                  {
                                  if (previous_text != '\n')
                                          putchar(text);
                                  else if (text!=ltr1 && text!=ltr2 && text!=ltr3 && text!=ltr4 && text!=ltr5 && text!= ' ')
                                                  putchar(text);
                                  else if (text==' ' && spaces==0)
                                                    ++spaces;
                                                     putchar(text);
                                                     previous_text = text;
                  
                                  }
                  
                          printf("\n");
                          }
                  }
                  
                  int main(void)
                  {
                          char a;
                          char e;
                          char i;
                          char o;
                          char u;
                  
                          erase(a,i,e,o,u);
                          return(0);
                  }
                  There were 3 errors in your code. First, you declared text and previous_text as integers, but they should be characters. Next, when you tried to initialize previous_text, you left out the underscore and wrote previous text, which was incorrect. Finally, the escape sequence is not \O with the letter o, but \0 with a zero - this is why your compiler gave you an error.

                  Comment

                  • bobcats
                    New Member
                    • Jan 2007
                    • 13

                    #10
                    Thanks sir. There seems to be something wrong with the code because it only gives white spaces after getting the text input for me. Does the same happen with you?

                    Comment

                    • bobcats
                      New Member
                      • Jan 2007
                      • 13

                      #11
                      I hope that someone could help me with this one. We're supposed to write a program like this by now. I'm sorry if im being a little uptight with this.

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        I think it is because of the while() condition and the if() condition immediately following it. The while() will stop executing if text is ever a newline character - but the if() statement inside will only ever execute if text IS the newline character. In other words, nothing ever happens in this code.

                        With a few changes, here is the result I got:

                        Code:
                        What is going on
                        W
                        h
                        a
                        t
                        
                        i
                        s
                        
                        g
                        o
                        i
                        n
                        g
                        
                        o
                        n
                        Press any key to continue . . .
                        The code itself...I added cout statements because I don't know the syntax for printf - all it is is basically putchar(text).

                        Code:
                        #include <cstdio>
                        #include <iostream>
                        using namespace std;
                        
                        void erase(char ltr1, char ltr2, char ltr3, char ltr4, char ltr5) {
                            char text;
                            int spaces;
                            char previous_text;
                        
                            spaces = 0;
                            previous_text = '\0'; // added _; also, \O is wrong: correct is \0 with a zero
                            while( (text = getchar()) != '\n') {
                                if (previous_text != '\n') cout << text;
                                else if (text != ltr1 && text != ltr2 && text != ltr3 && text != ltr4 && text != ltr5 && text != ' ') cout << text;
                                else if (text == ' ' && spaces == 0) {
                                    ++spaces;
                                    cout << text;
                                }
                                previous_text = text;
                                printf("\n");
                            }
                        }
                        
                        int main(void)
                        {
                                char a;
                                char e;
                                char i;
                                char o;
                                char u;
                        
                                erase(a,i,e,o,u);
                                system("pause");
                                return(0);
                        }
                        I don't know what you may have to remove in order to convert this back to C.

                        Comment

                        • RedSon
                          Recognized Expert Expert
                          • Jan 2007
                          • 4980

                          #13
                          bobcats,

                          Why do you not ask your instructor these questions? If you have do they not help you as much as you would like? It seems as though if you are having this much trouble working on this assignment there might be others who are also having troubles. They would benefit from you starting a conversation in your classroom about the problems that you raise.

                          Whenever I take a class, I assume that if I have a question about something there is at least one other person who has a similar question but is too afraid of "sounding dumb" to ask. Remember, your instructor is going to be your best (and first) resource for problems regarding assignments. That is unless they are rude or mean to you.

                          Comment

                          • bobcats
                            New Member
                            • Jan 2007
                            • 13

                            #14
                            Thanks again sir Ganon11. I'll try to convert it back to C. Regarding your question sir RedSon, our instructor is very knowledgeable already with C such that he expects all of us to at least be on par. But I'm having a hard time right now because I dont have any background with C. I really appreciate all you guys helping me.

                            Comment

                            • bobcats
                              New Member
                              • Jan 2007
                              • 13

                              #15
                              Sir I made the following code for the program but it doesnt work. Im clueless with what I'm doing wrong. I hope somebody could help. Thanks.

                              Code:
                              #include <stdio.h>
                              #include <ctype.h>
                              
                              void erase(char v1,char v2,char v3,char v4,char v5,char v6,char v7,char v8,char v9,char v10)
                              {
                                      int spaces;
                                      int txt;
                                      int lst_txt;
                              
                                      spaces = 0;
                                      txt = getchar();
                                      lst_txt = '\0';
                                      while (txt != '\n')
                                      {
                                              if (txt == '\n'){
                                                      if (lst_txt != '\n')
                                                      putchar(txt);
                                                      }
                                              else if ((txt != v1) && (txt != v2) && (txt != v3) && (txt != v4) && (txt != v5) && (txt != v6) && (txt != v7) && (txt != v8) && (txt != v9) && (txt != v10) && (txt != ' '))
                                                      putchar(txt);
                                              else if ((txt == ' ') && (spaces == 0))
                                                      ++spaces;
                                                      putchar(txt);
                                                      lst_txt = txt;
                                      }
                              
                                      printf("\n");
                              }
                              
                              int main(void)
                              {
                                      char a,A,e,E,o,O,i,I,u,U;
                              
                              
                                      erase(char 'a';char 'A';char 'e';char 'E';char 'o';char 'O';char 'i';char 'I';char 'u';char 'U');
                                      return(0);
                              }

                              Comment

                              Working...