473,396 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help with program

13
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:
Expand|Select|Wrap|Line Numbers
  1. #include<ctype.h>
  2. #include<stdio.h>
  3.  
  4. void remove_vowel(char x;char y; char z; char v; char u)
  5. {
  6. int c, last_c = '\O';
  7. int d;
  8.  
  9. d = 1;
  10. while((c=getchar()) != '\n')
  11. { if (c == '\n'){
  12.     if (last_c != '\n')
  13.            putchar(c)
  14. }
  15. else if (c !=x && c !=y && c !=z && c !=u && c !=x && c !='')
  16.     putchar(c);
  17. else if (c == '' && d == 1)
  18. {  ++d;
  19.    putchar(c);
  20.    last_c =c;
  21. }
  22. }
  23.  
  24. printf("\n")
  25. }
  26.  
  27. int main(void)
  28. {
  29. char a,e,i,o,u;
  30.  
  31. remove_vowel(char 'a';char 'e';char 'i';char 'o';char 'u';)
  32. return(0);
  33. }
  34.  
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.
Jan 29 '07 #1
17 1534
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. #include<ctype.h>
  2. #include<stdio.h>
  3.  
  4. void remove_vowel(char x;char y; char z; char v; char u)
  5. {
  6.    int c, last_c = '\0';
  7.    int d;
  8.  
  9.    d = 1;
  10.    while((c=getchar()) != '\n')
  11.    {
  12.       if (c == '\n')
  13.       {
  14.          if (last_c != '\n') putchar(c)
  15.       }
  16.       else if (c !=x && c !=y && c !=z && c !=u && c !=x && c !='') putchar(c);
  17.       else if (c == '' && d == 1)
  18.       {
  19.          ++d;
  20.          putchar(c);
  21.          last_c =c;
  22.       }
  23.    }
  24.  
  25.    printf("\n")
  26. }
  27.  
  28. int main(void)
  29. {
  30.    char a,e,i,o,u;
  31.  
  32.    remove_vowel(char 'a';char 'e';char 'i';char 'o';char 'u';)
  33.    return(0);
  34. }
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.

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.

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.

4)Is there anyway to emulate the program with the use of only basic functions and loops?
Why don't you try?
Jan 29 '07 #2
bobcats
13
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.
Jan 30 '07 #3
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. #include<ctype.h>
  2. #include<stdio.h>
  3.  
  4. void remove_vowel(char x;char y; char z; char v; char u)
  5. {
  6.    int c, last_c = '\0';
  7.    int d;
  8.  
  9.    d = 1;
  10.    while((c=getchar()) != '\n')
  11.    {
  12.       if (c == '\n')
  13.       {
  14.          if (last_c != '\n') putchar(c)
  15.       }
  16.       else if (c !=x && c !=y && c !=z && c !=u && c !=x && c !='') putchar(c);
  17.       else if (c == '' && d == 1)
  18.       {
  19.          ++d;
  20.          putchar(c);
  21.          last_c =c;
  22.       }
  23.    }
  24.  
  25.    printf("\n")
  26. }
  27.  
  28. int main(void)
  29. {
  30.    char a,e,i,o,u;
  31.  
  32.    remove_vowel(char 'a';char 'e';char 'i';char 'o';char 'u';)
  33.    return(0);
  34. }
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.

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.
Jan 30 '07 #4
bobcats
13
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
Jan 30 '07 #5
Ganon11
3,652 Expert 2GB
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.

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.
Jan 30 '07 #6
bobcats
13
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.
Jan 30 '07 #7
bobcats
13
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.

Expand|Select|Wrap|Line Numbers
  1. #include<ctype.h>
  2. #include<stdio.h>
  3.  
  4. void erase(char ltr1, char ltr2, char ltr3, char ltr4, char ltr5)
  5.  
  6. {
  7.         int text;
  8.         int spaces;
  9.         int previous_text;
  10.  
  11.         spaces = 0;
  12.         previous text = '\O';
  13.         while( (text=getchar() ) != '\n')
  14.         { if (text == '\n')
  15.                 {
  16.                 if (previous_text != '\n')
  17.                         putchar(text);
  18.                 else if (text!=ltr1 && text!=ltr2 && text!=ltr3 && text!=ltr4 && text!=ltr5 && text!= ' ')
  19.                                 putchar(text);
  20.                 else if (text==' ' && spaces==0)
  21.                                   ++spaces;
  22.                                    putchar(text);
  23.                                    previous_text = text;
  24.  
  25.                 }
  26.  
  27.         printf("\n");
  28.         }
  29. }
  30.  
  31. int main(void)
  32. {
  33.         char a;
  34.         char e;
  35.         char i;
  36.         char o;
  37.         char u;
  38.  
  39.         erase(a,i,e,o,u);
  40.         return(0);
  41. }
  42.  
  43.  
Jan 31 '07 #8
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. #include<ctype.h>
  2. #include<stdio.h>
  3.  
  4. void erase(char ltr1, char ltr2, char ltr3, char ltr4, char ltr5)
  5.  
  6. {
  7.         char text;
  8.         int spaces;
  9.         char previous_text;
  10.  
  11.         spaces = 0;
  12.         previous_text = '\0'; // added _; also, \O is wrong: correct is \0 with a zero
  13.         while( (text=getchar() ) != '\n')
  14.         { if (text == '\n')
  15.                 {
  16.                 if (previous_text != '\n')
  17.                         putchar(text);
  18.                 else if (text!=ltr1 && text!=ltr2 && text!=ltr3 && text!=ltr4 && text!=ltr5 && text!= ' ')
  19.                                 putchar(text);
  20.                 else if (text==' ' && spaces==0)
  21.                                   ++spaces;
  22.                                    putchar(text);
  23.                                    previous_text = text;
  24.  
  25.                 }
  26.  
  27.         printf("\n");
  28.         }
  29. }
  30.  
  31. int main(void)
  32. {
  33.         char a;
  34.         char e;
  35.         char i;
  36.         char o;
  37.         char u;
  38.  
  39.         erase(a,i,e,o,u);
  40.         return(0);
  41. }
  42.  
  43.  
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.
Jan 31 '07 #9
bobcats
13
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?
Jan 31 '07 #10
bobcats
13
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.
Jan 31 '07 #11
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1. What is going on
  2. W
  3. h
  4. a
  5. t
  6.  
  7. i
  8. s
  9.  
  10. g
  11. o
  12. i
  13. n
  14. g
  15.  
  16. o
  17. n
  18. 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).

Expand|Select|Wrap|Line Numbers
  1. #include <cstdio>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void erase(char ltr1, char ltr2, char ltr3, char ltr4, char ltr5) {
  6.     char text;
  7.     int spaces;
  8.     char previous_text;
  9.  
  10.     spaces = 0;
  11.     previous_text = '\0'; // added _; also, \O is wrong: correct is \0 with a zero
  12.     while( (text = getchar()) != '\n') {
  13.         if (previous_text != '\n') cout << text;
  14.         else if (text != ltr1 && text != ltr2 && text != ltr3 && text != ltr4 && text != ltr5 && text != ' ') cout << text;
  15.         else if (text == ' ' && spaces == 0) {
  16.             ++spaces;
  17.             cout << text;
  18.         }
  19.         previous_text = text;
  20.         printf("\n");
  21.     }
  22. }
  23.  
  24. int main(void)
  25. {
  26.         char a;
  27.         char e;
  28.         char i;
  29.         char o;
  30.         char u;
  31.  
  32.         erase(a,i,e,o,u);
  33.         system("pause");
  34.         return(0);
  35. }
  36.  
I don't know what you may have to remove in order to convert this back to C.
Jan 31 '07 #12
RedSon
5,000 Expert 4TB
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.
Jan 31 '07 #13
bobcats
13
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.
Feb 1 '07 #14
bobcats
13
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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. void erase(char v1,char v2,char v3,char v4,char v5,char v6,char v7,char v8,char v9,char v10)
  5. {
  6.         int spaces;
  7.         int txt;
  8.         int lst_txt;
  9.  
  10.         spaces = 0;
  11.         txt = getchar();
  12.         lst_txt = '\0';
  13.         while (txt != '\n')
  14.         {
  15.                 if (txt == '\n'){
  16.                         if (lst_txt != '\n')
  17.                         putchar(txt);
  18.                         }
  19.                 else if ((txt != v1) && (txt != v2) && (txt != v3) && (txt != v4) && (txt != v5) && (txt != v6) && (txt != v7) && (txt != v8) && (txt != v9) && (txt != v10) && (txt != ' '))
  20.                         putchar(txt);
  21.                 else if ((txt == ' ') && (spaces == 0))
  22.                         ++spaces;
  23.                         putchar(txt);
  24.                         lst_txt = txt;
  25.         }
  26.  
  27.         printf("\n");
  28. }
  29.  
  30. int main(void)
  31. {
  32.         char a,A,e,E,o,O,i,I,u,U;
  33.  
  34.  
  35.         erase(char 'a';char 'A';char 'e';char 'E';char 'o';char 'O';char 'i';char 'I';char 'u';char 'U');
  36.         return(0);
  37. }
  38.  
  39.  
Feb 1 '07 #15
bobcats
13
Um sir my code doesnt do the desired job which is to wait for the text input and to give an output without vowels and only one space.
Example:
Where are you going?
Whr rygng?
Our prof just said that we had to pass a program like this by the end of the week. Hope someone could help me again. My apologies for sounding a little desperate.
Feb 1 '07 #16
Ganon11
3,652 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. #include <cstdio>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. void erase(char ltr1, char ltr2, char ltr3, char ltr4, char ltr5) {
  6.     char text;
  7.     int spaces;
  8.     char previous_text;
  9.  
  10.     spaces = 0;
  11.     previous_text = '\0'; // added _; also, \O is wrong: correct is \0 with a zero
  12.     while( (text = getchar()) != '\n') {
  13.         if (previous_text != '\n') cout << text;
  14.         else if (text != ltr1 && text != ltr2 && text != ltr3 && text != ltr4 && text != ltr5 && text != ' ') cout << text;
  15.         else if (text == ' ' && spaces == 0) {
  16.             ++spaces;
  17.             cout << text;
  18.         }
  19.         previous_text = text;
  20.         printf("\n");
  21.     }
  22. }
  23.  
  24. int main(void)
  25. {
  26.         char a;
  27.         char e;
  28.         char i;
  29.         char o;
  30.         char u;
  31.  
  32.         erase(a,i,e,o,u);
  33.         system("pause");
  34.         return(0);
  35. }
I think the only problem with this code is within the main() function. You declare five character variables called a, e, i, o, and u, but you never set them to anything - they are uninitialized when you pass them to erase.

I changed the main function to

Expand|Select|Wrap|Line Numbers
  1. int main(void)
  2. {
  3.         char a = 'a';
  4.         char e = 'e';
  5.         char i = 'i';
  6.         char o = 'o';
  7.         char u = 'u';
  8.  
  9.         erase(a,i,e,o,u);
  10.         system("pause");
  11.         return(0);
  12. }
and got rid of the very first if...statement in the function, and it now performs what you want.
Feb 1 '07 #17
bobcats
13
Hi again sir. I finally got the code right. i just having some trouble with the concepts We may need to answer some questions about the code. I posted the correct code and a wrong one.
My questions are:
1) why cant be the getchar be declared = text just like in the wrong code? is it because it is not a constant?
2) why is there a need for a counter for the previous text entered?
3. why should the function type be void?
Thanks for the help. Hope you could answer my hopefully last questions.

Correct code:
Expand|Select|Wrap|Line Numbers
  1. #include<ctype.h>
  2. #include<stdio.h>
  3.  
  4. void erase(char v1,char v2,char v3,char v4,char v5,char v6,char v7,char v8,char v9,char v10)
  5.  
  6. {
  7.         int txt;
  8.         int prev_txt = '\0';
  9.         int spaces;
  10.  
  11.         spaces = 0;
  12.         while((txt=getchar()) != '\n')
  13.         { if (txt == '\n'){
  14.                                         if (prev_txt != '\n')
  15.                                                               putchar(txt);
  16.                         }
  17.         else if (txt !=v1 && txt !=v2 && txt !=v3 && txt !=v4 && txt !=v5 && txt !=v6 && txt !=v7 && txt !=v8 && txt !=v9 && txt !=v10 && txt !=' ' && txt !='\t')
  18.                 putchar(txt);
  19.         else if (txt == ' ' && spaces == 0)
  20.                 {  ++(spaces);
  21.                    putchar(txt);
  22.               prev_txt =txt;
  23.                 }
  24.         }
  25.  
  26.         printf("\n");
  27. }
  28.  
  29. int main(void)
  30. {
  31.         char a = 'a'; char A = 'A';
  32.         char e = 'e'; char E = 'E';
  33.         char i = 'i'; char I = 'I';
  34.         char u = 'u'; char O = 'O';
  35.         char o = 'o'; char U = 'U';
  36.  
  37.  
  38.         erase(u,e,i,o,a,A,E,I,O,U);
  39.                 return(0);
  40. }
  41.  
  42.  
Wrong Code:
Expand|Select|Wrap|Line Numbers
  1. #include<ctype.h>
  2. #include<stdio.h>
  3.  
  4. void erase(char v1,char v2,char v3,char v4,char v5,char v6,char v7,char v8,char v9,char v10)
  5.  
  6. {
  7.         int txt;
  8.         int prev_txt = '\0';
  9.         int spaces;
  10.  
  11.         spaces = 0;
  12.         txt=getchar();
  13.         while(txt!= '\n')
  14.         { if (txt == '\n'){
  15.                                         if (prev_txt != '\n')
  16.                                                               putchar(txt);
  17.                         }
  18.         else if (txt !=v1 && txt !=v2 && txt !=v3 && txt !=v4 && txt !=v5 && txt !=v6 && txt !=v7 && txt !=v8 && txt !=v9 && txt !=v10 && txt !=' ' && txt !='\t')
  19.                 putchar(txt);
  20.         else if (txt == ' ' && spaces == 0)
  21.                 {  ++(spaces);
  22.                    putchar(txt);
  23.               prev_txt =txt;
  24.                 }
  25.         }
  26.  
  27.         printf("\n");
  28. }
  29.  
  30. int main(void)
  31. {
  32.         char a = 'a'; char A = 'A';
  33.         char e = 'e'; char E = 'E';
  34.         char i = 'i'; char I = 'I';
  35.         char u = 'u'; char O = 'O';
  36.         char o = 'o'; char U = 'U';
  37.  
  38.  
  39.         erase(u,e,i,o,a,A,E,I,O,U);
  40.                 return(0);
  41. }
  42.  
  43.  
Feb 1 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
5
by: Bec | last post by:
I'm in desperate need of your help.. I need to build an access database and have NO idea how to do this.. Not even where to start.. It IS for school, and am not asking anyone to do my...
7
by: tyler_durden | last post by:
thanks a lot for all your help..I'm really appreciated... with all the help I've been getting in forums I've been able to continue my program and it's almost done, but I'm having a big problem that...
2
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
2
by: Bsnpr8 | last post by:
I need help guys, i have to many stuff to do, because i am in my last 2 weeks of the university, my last assignment is to do a spell checker in C++, i have an idea but nothing is coming out. I really...
6
by: HelpME | last post by:
I wrote a program in Vb.Net that was running fine. However I am unable to install it on a couple of machines. When i run it I get a windows error message that says My Project.exe has...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.