I am trying to solve an assignment question - "Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank". After writing the code, I am trying to compile it on a Mac terminal. I know how to compile using gcc command.
How do I give input from terminal to see what the output is?
The code that I have tried:
#include <stdio.h>
int main(void)
{
int c, inspace;
inspace = 0;
while((c = getchar()) != EOF)
{
if(c == ' ')
{
if(inspace ==0)
{
inspace = 1;
putchar(c);
}
}
if(c != ' ')
{
inspace = 0;
putchar(c);
}
}
return 0;
}
Use
gcc program.c
then run using :
./a.out
don't give enter, first type in some characters using your keyboard, then press enter. You'd need to type control-D at the beginning of a line to indicate EOF (or type it twice to indicate EOF without a newline as the last character). You could also interrupt the program with control-C.
Related
I'm writing a program which takes input and outputs it one word per line.
#include <stdio.h>
int main()
{
int c;
while( (c = getchar()) != EOF)
{
if( c != ' ')
putchar(c);
else
putchar('\n');
}
}
The program stops when it encounters the EOF character. When I input something like "Hello World^Z" (I am using Windows) I get the following output:
C:\Coding\C>a
Hello World^Z
Hello
World→
And it still waits for me to input something. What does the → symbol mean?
#include <stdio.h>
int main() {
int c;
while(getchar() != EOF) {
if (getchar() == ' ') {
c++;
}
printf("%i", c);
}
}
I realized that typing in a sentence like the one you're reading right
I\nrealized\nthat\ntyping\nin\n\a\n ...
i believe that's how it's being read, getchar() does not reach the EOF to make the condition in the while parentheses false..
my goal here is to make a program that takes in input from me..
reads it
if there are any spaces
it counts on a counter
when EOF is reached
the condition to keep reading it becomes false
the counter value gets printed out on the screen
to show me how many spaces i had in my entire input..
is it impossible? is that why people just use scanf() ?
this is the output i get when trying something
user#user:/c# ./a.out
hello stackoverflow this does not do what i want it to
001111111222223344445666677
You need to put the result of getchar() into a variable:
int ch;
while ((ch = getchar()) != EOF)
You shouldn't call getchar() a second time to check if it's a space, since that will read a second character so you'll be testing every other character, just compare the variable:
if (ch == ' ')
And if you want to see the total number of spaces, put the printf() at the end of the loop, not inside it.
So the whole thing should look like:
#include <stdio.h>
int main() {
int counter=0;
int ch;
while((ch = getchar()) != EOF) {
if (ch == ' ') {
counter++;
}
}
printf("%i\n", counter);
}
To send EOF from the terminal, type Control-d on Unix, Control-z on Windows.
Run command sequence:
gcc -Wall main.c -o a.out
./a.out < inputfile.txt
I want to read from file like ./a.out < inputfile.txt and after iterating char by char, print OK if all the characters are alphanumeric or ERROR otherwise. I can't seem to get this to work.
inputfile.txt
the brown fox jumped over the dog
this is another string
here is another string
main.c
int main() {
char c;
while (!feof(stdin)) {
c = getchar();
if (!isalnum(c)) {
printf("ERROR!\n");
exit(1);
}
}
printf("OK\n);
return 0;
}
As the others said - spaces and new lines are not alnum characters. Use isalnum() + isspace() in this case. In addition to that consider using some kind of a flag instead of using exit() function:
#include <stdio.h>
#include <ctype.h>
int main()
{
int c;
char ok='Y';
while(c = getchar())
{
if(c == EOF) break;
if(!isalnum(c) && !isspace(c))
{
ok = 'N';
break;
}
}
printf("%c\n", ok);
return 0;
}
RTFM: http://www.cplusplus.com/reference/cctype/
I swear this is the last time when I'm gonna help out people who can't even debug their codes.
A space is not an alphanumeric character.
See the table in this page for what is or isn't "isalnum".
You're using feof wrong. feof returns true when the EOF indicator has already been set, i.e: when you have already read an EOF. So when stdin reaches end-of-file you still get one loop iteration with EOF, which isn't an alpha-numeric character. In order to make sure you can properly distinguish EOF from any valid character you should declare c as an int. I suggest:
int c = getchar();
while(c != EOF){
if(!isalnum(c)){
printf("ERROR!\n");
exit(1);
}
c = getchar();
}
printf("OK\n");
#include <stdio.h>
/* replacing tabs and backspaces with visible characters */
int main()
{
int c;
while ( (c = getchar() ) != EOF) {
if ( c == '\t')
printf("\\t");
else if ( c == '\b')
printf("\\b");
else if ( c == '\\')
printf("\\\\");
else
putchar(c);
}
return 0;
}
Now my question is .. Why can't I see "\b" in the output ? I have written this code in Ubuntu terminal. Is there any other way to get the "\b" character in output ? If there is , please explain in simple words as I have just started learning C programming.This example is from K&R exercise 1-10.
Run the program and enter CtrlH.
The key-code send by the backspace key (also: <---) is most probably eaten by the shell. This depends on how the terminal is configured. Read here for details: http://www.tldp.org/HOWTO/Keyboard-and-Console-HOWTO-5.html
Is there any other way to get the "\b" character in output ?
If you are supplying the input like:
abbackspacecd
to your program then it'd result in abc because that is what the shell passed to the program.
Ensure that you send the correct input to the program. Invoke it by saying:
printf $'ab\bcd' | /path/to/executable
and it'd print the expected output, i.e.:
ab\bcd
This is not valid C code. It should look like this:
#include <stdio.h>
/* replacing tabs and backspaces with visible characters */
int main()
{
int c;
while ( (c = getchar() ) != EOF) {
if ( c == '\t')
printf("\\t");
else if ( c == '\b')
printf("\\b");
else if ( c == '\\')
printf("\\\\");
else
putchar(c);
} // <-- note the closing curly brace
return 0;
}
You should prepare a file containing the \b (0x08) and use it as input for your program. Another way would be to press Ctrl-H and then Enter (Thanks #alk for the key combination)
I have already looked at this similar question but i am still wondering if there is another way to stop 1) the terminal echoing with portabilty as this is an assignment and I have already had one java program crash and burn on my teachers computer 2) in my program i search for a '\n' char then if it isn't the first char use getchar then putchar till the next '\n' char which works fine when using redirected stdin but when I try using the program without redirection the enter key is always echoed, is this to do with the terminal echoing or do i need to check for a diffrent char apart from '\n'? I have also tried including '/r' and done lots of googling but it seems the answer to the echo is can't be done with portabilty?
#include <stdio.h>
#include <string.h>
int first_line(char);
int main(){
char c;
while((c = getchar())!=EOF){
first_line(c);
}
return 0;
}
int first_line(char c){
if (c != '\n'||c != '\r'){
putchar(c);
do{
c = getchar();
putchar(c);}
while( c !='\n');
}
return 0;
}
Thanks Lachlan
For a start try with the following :
1) the condition should be if (c != '\n' && c != '\r')
2) and the while loop ,in case if terminal is line buffered then you are better of using getchfrom ncurses library the library packages should be there for most platforms.
while((c =getchar())!='\n') {
putchar(c);
}