This program should replace two spaces with an x, using only getchar() and putchar(). My approach was to store the space in a buffer and then print it out. But the program replaces every space with an x. Can someone help me out?
#include <stdio.h>
#define MAX 2
char arr[MAX];
int ret = 0;
char second;
int main()
{
for(int i=0; ; )
{
if ( (ret = getchar())!= EOF)
{
putchar(ret);
}
if(ret==' '&&second==' ')
{
arr[i]=ret;
arr[i]='x';
putchar(arr[i]);
}
}
return 0;
}
When you read a character, first check if it's a space. If not, just print it. If it is read another character, then if the second is a space print an x otherwise print a space and the character you just read.
int c;
while ((c = getchar()) != EOF) {
if (c != ' ') {
putchar(c);
} else {
c = getchar();
if (c == EOF) {
putchar(' ');
} else if (c == ' ') {
putchar('x');
} else {
putchar(' ');
putchar(c);
}
}
}
Related
I wrote this program that replaces two spaces with an '*'.
How do I modify the code so that it does the same thing regardless of the string size? Is it even possible only using putchar and getchar?
#include <stdio.h>
int c;
char buffer[256];
int counter= 0;
int i;
int main()
{
while ((c = getchar()) != '\n'&&c!=EOF) {
buffer[counter] =c;
counter++;
if (counter >=255) {
break;
}
}
for(i=0; i<256; i++) {
if(buffer[i]== ' '&&buffer[i+1]==' ')
{
buffer[i]= '*';
putchar(buffer[i]);
i = i + 2;
continue;
}
putchar(buffer[i]);
}
putchar('\n');
return 0;
}
The problem statement doesn't require you to store the complete input in a buffer. The decision on what character to output only depends on the last two characters of input. Consider the following code:
#include <stdio.h>
int main(void)
{
// two variables for the last two input characters
int c = EOF, last = EOF;
while ((c = getchar()) != EOF)
{
// if both are a space, store a single '*' instead
if (c == ' ' && last == ' ')
{
c = '*';
last = EOF;
}
// print the output, and shift the buffer
if (last != EOF)
putchar(last);
last = c;
}
// take care of the last character in the buffer after we see EOF
if (last != EOF)
putchar(last);
}
no need for malloc and friends at all. This is a good expample for a problem that requires you to think carefully, before writing code, in order to not waste unnecessary resources on buffers.
Code for just printing:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char prev = EOF, curr;
while ((curr =(char)getchar()) != '\n' && curr != EOF)
{
if(curr==' '&&prev==' ')
{
curr = '*';
prev = EOF;
}
if (prev != EOF)
putchar(prev);
prev = curr;
}
putchar(prev);
return 0;
}
Using realloc for actually changing the string:
#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned int len_max = 128;
char *m = malloc(len_max);
char c;
int counter= 0;
int i;
int current_size = 256;
printf("please input a string\n");
while ((c = getchar()) != '\n' && c != EOF)
{
m[counter] =(char)c;
counter++;
if(counter == current_size)
{
current_size = i+len_max;
m = realloc(m, current_size);
}
}
for(i=0; i<counter; i++)
{
if(m[i]== ' '&&m[i+1]==' ')
{
m[i]= '*';
putchar(m[i]);
i = i + 2;
continue;
}
putchar(m[i]);
}
putchar('\n');
free(m);
m = NULL;
return 0;
}
I'm exercise code. The thing is, to replace space from input, with a new line.
I wrote something like this:
int input;
while((input = getchar()) != EOF)
{
if (input == ' ')
{
input = '\n';
}
putchar(input);
}
But I don't know how to make it change more than one space into exactly one new line. I had an idea to make something like buffer variable (ex. int buffer) and store space in there, and then check if after input, previous character was space, but I don't have any idea how to make it work :P
int input, last_was_space = 0;
while((input = getchar()) != EOF)
{
if(input == ' ')
{
last_was_space = 1;
}
else
{
if(last_was_space)
{
last_was_space = 0;
putchar('\n');
}
putchar(input);
}
}
Use this instead of the if statement:
if(input == ' ') {
while (input == ' ')
{
input = getchar();
}
putchar('\n');
}
Perhaps this will work for you, though the newline won't show up until the input ends or the first non-space character is entered.
int input = 0;
int previous = 0;
while((input = getchar()) != EOF)
{
if (input != ' ')
{
if (previous == ' ')
{
putchar('\n');
}
putchar(input);
}
previous = input;
}
if (input == ' ')
{
putchar('\n');
}
I am new to programming, and I have decided to start with c. I am using the book of K & R, and there is this exercise, which asks to write a program that copies input to output , replacing one or more blanks with a single blank. However when I wrote my program (and I am sure it's not correct but that's okay, since I am here to learn) I was wondering what I am doing wrong. Also a note: when I type my name with 3 blanks it's reduced to two, but when using two or one blank(s) nothing happens. Code posted below
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c; // getchar value
int blanks = 0; // counting the amount of blanks. If more than one then replace with blanks_2
char blanks_2 = ' '; //character value for replacement in case blanks is more than one
printf("Enter your name please\n");
while((c = getchar()) != EOF){
if(c == ' '){
++blanks;
if (blanks >= 1){
putchar(blankos); }
}
if(c >= 'a' && c <= 'z'){
putchar(c);
}
if (c >= 'A' && c <= 'Z'){
putchar(c);
}
}
return 0;
}
You do not need to count the exact number of blanks. Once you find a blank "raise a flag" that blank is found. When you revisit a non-blank character print a blank and turn the flag back to 0. Moreover, insert "continue" statements in your code to avoid unecessary checks:
int main(void)
{
int c; // getchar value
int blankfound = 0;
printf("Enter your name please\n");
while((c = getchar()) != EOF){
if(c == ' '){
blankfound = 1;
continue;
}
if(c >= 'a' && c <= 'z'){
if (blankfound == 1)
{
putchar(' ');
blankfound = 0;
}
putchar(c);
continue;
}
if (c >= 'A' && c <= 'Z'){
if (blankfound == 1)
{
putchar(' ');
blankfound = 0;
}
putchar(c);
continue;
}
}
return 0;
}
I guess you are trying to write a program that get something like this as input:
John David Doe
and display this as output:
John David Doe
by removing all extra spaces. This should work:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int c; // getchar value
int blanks = 0; // counting the amount of blanks. If more than one then replace with blanks_2
printf("Enter your name please\n");
while((c = getchar()) != EOF)
{
if(c == ' ')
{
// count blanks
++blanks;
if (blanks==1)
putchar(c); // display only the 1st blank
}
if(isalpha(c))
{
putchar(c);
blanks=0; // reset blanks counter as c is an alpha character
}
}
return 0;
}
Many of these learner code assignments work on the idea of function based on a previous character.
Consider the following layout
int previous = something();
while ((c = getchar()) != EOF) {
do_stuff(previous, c);
previous = c;
}
For OP, that would be: If the character is not a space or the previous character was not a space, print it.
printf("Enter your name please\n");
int previous = 0;
int c;
while((c = getchar()) != EOF) {
if ((c != ' ') || (previous != ' ')) {
putchar(' ');
}
previous = c;
}
Simplifying the algorithm helps one see flaws like below. blanks is not reset when a letter appears. It prints when 1 or more spaces encountered.
if(c == ' '){
++blanks;
if (blanks >= 1){
putchar(blankos); }
}
if(c >= 'a' && c <= 'z'){
putchar(c);
}
if (c >= 'A' && c <= 'Z'){
putchar(c);
}
Reference to previous chapters (Used only 'while' and 'if'), my code look like this.
#include <stdio.h>
main()
{
int c;
while ((c = getchar()) != EOF) {
if (c == ' ') {
putchar(c);
while ((c = getchar()) == ' ')
;
}
putchar(c);
}
}
#include <stdlib.h>
#include <stdio.h>
int main()
{
unsigned long c;
unsigned long line;
unsigned long word;
char ch;
c = 0;
line = 0;
word = 0;
while((ch = getchar()) != EOF)
{
c ++;
if (ch == '\n')
{
line ++;
}
if (ch == ' ' || ch == '\n' || ch =='\'')
{
word ++;
}
}
printf( "%lu %lu %lu\n", c, word, line );
return 0;
}
My program works fine for the most part, but when I add extra spaces, it counts the spaces as extra words. So for example, How are you? is counted as 10 words, but I want it to count as 3 words instead. How could I modify my code to get it to work?
I found a way to count words and between them several spaces the program will count only the words and not the several spaces also as words
here is the code:
nbword is the number of words, c is the character typed and prvc is the previously typed character.
#include <stdio.h>
int main()
{
int nbword = 1;
char c, prvc = 0;
while((c = getchar()) != EOF)
{
if(c == ' ')
{
nbword++;
}
if(c == prvc && prvc == ' ')
nbword-;
if(c == '\n')
{
printf("%d\n", nbword);
nbword = 1:
}
prvc = c;
}
return 0:
}
This is one possible solution:
#include <stdlib.h>
#include <stdio.h>
int main()
{
unsigned long c;
unsigned long line;
unsigned long word;
char ch;
char lastch = -1;
c = 0;
line = 0;
word = 0;
while((ch = getchar()) != EOF)
{
c ++;
if (ch == '\n')
{
line ++;
}
if (ch == ' ' || ch == '\n' || ch =='\'')
{
if (!(lastch == ' ' && ch == ' '))
{
word ++;
}
}
lastch = ch;
}
printf( "%lu %lu %lu\n", c, word, line );
return 0;
}
Hope this helped, good luck!
i was wondering if i could get some help for my code. I put some partial code below
/*reads char by char til EOF*/
while((c = getchar()) != EOF)
{
if(c == '\t')
{
putchar(' ');
}
else if(c == ' ')
{
putchar('d');
}
else
{
putchar(c);
}
}
What I am trying to do right now is squeeze space characters entered by the user. So if the user puts in:
a[SPACE][SPACE][SPACE][SPACE][SPACE][SPACE][SPACE][SPACE]a
The output should be just
a[SPACE]a
Right now i have it set up that it replaces all spaces for d's for testing purposes. How would I change my code so that it just prints out 1 space instead of all the spaces the user puts in.
Thanks for any help in advance.
Just keep a whitespace flag:
int lastWasSpace = 0;
while((c = getchar()) != EOF) {
if(c == '\t' || c == ' ') { // you could also use isspace()
if(!lastWasSpace) {
lastWasSpace = 1;
putchar(c);
}
} else {
lastWasSpace = 0;
}
}
One solution:
/*reads char by char til EOF*/
int hasspace = 0;
while((c = getchar()) != EOF)
{
if (isspace(c))
hasspace = 1;
}
else
{
if (hasspace)
{
hasspace = 0;
putchar(' ');
}
putchar(c);
}
}
First things first, how have you declared c?:
while((c = getchar()) != EOF)
If c is a char, then it cannot hold all characters and an EOF. Be sure c is declared with a datatype larger than char (int is usual).
Next, you can handle compressing multiple spaces with a cheap trick:
int space_seen = 0;
while((c = getchar()) != EOF)
{
if(c == '\t')
{
putchar(' ');
}
else if(c == ' ')
{
if (!space_seen)
{
putchar('d');
space_seen = 1;
}
}
else
{
putchar(c);
space_seen = 0;
}
}
This trick is also good for keeping track of parsing strings, too.
jcomeau#intrepid:/tmp$ cat compress.c; echo 'this is a test' | ./compress
#include <stdio.h>
int main() {
int c, lastchar = 'x';
while ((c = getchar()) != EOF) {
if (c == '\t' || c == ' ') {
if (lastchar != ' ') {
putchar(' ');
lastchar = ' ';
}
} else {
putchar(c);
lastchar = c;
}
}
}
this is a test
Record when you printed a space, and don't print them anymore until you find another letter.
Using your code as a base:
unsigned char space = 0;
/* reads char by char until EOF */
while((c = getchar()) != EOF)
{
if(c == '\t')
{
putchar(' ');
}
else if(c == ' ')
{
/* state specific action */
if(space == 0) {
putchar('d');
space = 1; /* state transition */
}
}
else
{
/* state transition */
if(space == 1) {
space = 0;
}
putchar(c);
}
}
There you go. A very, very simple state machine. It's easy as that!