getchar

Using fgets() right after getchar()

ⅰ亾dé卋堺 提交于 2021-02-17 05:21:39
问题 void read_stdin(trace_t* trace, state_t state, action_t** action_list) { // initial stage int c; while ((c = getchar())!= EOF && c!='#') { if (my_isalpha(c)==LOWERCASE) { state[c-ASCII_CODE_LOWER_A] = '1'; } } printf("%s\n", state); char str[2]; fgets(str, 2, stdin); printf("%s", str); } If '#' is the last character I enter in the getchar() loop, fgets() records the newline character from when I press enter and skips to the print statement immediately (which prints the '\n') I could fix this

Removing multiple blanks using putchar and getchar in C

こ雲淡風輕ζ 提交于 2021-02-16 18:24:27
问题 Problem: Write a program that receives textual input using getchar() and outputs the string, having removed multiples blanks. Here's how I wrote the pseudo-code: While each input character is received before reaching EOF, do the following: 1) if character is non-blank, print it out 2) otherwise: a. print out the blank b. do nothing untill the next non-blank character 3) if a non-blank character is reached, go back to 1) I tried to implement the algorithm as such: #include <stdio.h> /*

static char inpbuf[MAXBUF], tokbuf[2 * MAXBUF], *ptr = inpbuf, *tok = tokbuf;

独自空忆成欢 提交于 2021-01-07 02:47:45
问题 static char inpbuf[MAXBUF], tokbuf[2 * MAXBUF], *ptr = inpbuf, *tok = tokbuf; The array inpbuf is intended to store the user typed command and its arguments. The array tokbuf is intended to store each extracted tokens from inpbuf , such that, each element in the array char *arg[MAXARG + 1]; (in proc_line.c) is properly assigned using the information in tokbuf in order later on to invoke execvp system call in runcommand.c. I wanted to get rid of tokbuf and make the program still work. I

static char inpbuf[MAXBUF], tokbuf[2 * MAXBUF], *ptr = inpbuf, *tok = tokbuf;

馋奶兔 提交于 2021-01-07 02:45:19
问题 static char inpbuf[MAXBUF], tokbuf[2 * MAXBUF], *ptr = inpbuf, *tok = tokbuf; The array inpbuf is intended to store the user typed command and its arguments. The array tokbuf is intended to store each extracted tokens from inpbuf , such that, each element in the array char *arg[MAXARG + 1]; (in proc_line.c) is properly assigned using the information in tokbuf in order later on to invoke execvp system call in runcommand.c. I wanted to get rid of tokbuf and make the program still work. I

Why is getchar() function in C an Integer? [duplicate]

限于喜欢 提交于 2020-07-16 05:49:31
问题 This question already has answers here : Difference between int and char in getchar/fgetc and putchar/fputc? (2 answers) Closed 2 years ago . Up to now, i have found out that, we use getchar() as int because at EOF this function returns -1. I was wondering can't char hold -1 ? i think it can,because it can also range from -128 to 127. I searched through the top list of google, but the answer i got didn't satisfy me. 回答1: First of all, just to clear something up, EOF is not required to be

Reading input from getchar() giving unexpected results

喜夏-厌秋 提交于 2020-05-31 11:45:23
问题 I have a function which reads PIN from terminal and stores PIN and PIN length in the variables passed. On the first call to the function I get expected PIN and PIN length entered. However, during the second call to this function the first character is omitted. /* * Function : read_pin(char *pin,int *pin_len) * Description : Read entered PIN and stores PIN in pin and pin length in pin_len */ int read_pin(unsigned char *pin,unsigned int *pin_len) { int err = EXIT_SUCCESS; char ch; fflush(stdout

Reading input from getchar() giving unexpected results

回眸只為那壹抹淺笑 提交于 2020-05-31 11:44:03
问题 I have a function which reads PIN from terminal and stores PIN and PIN length in the variables passed. On the first call to the function I get expected PIN and PIN length entered. However, during the second call to this function the first character is omitted. /* * Function : read_pin(char *pin,int *pin_len) * Description : Read entered PIN and stores PIN in pin and pin length in pin_len */ int read_pin(unsigned char *pin,unsigned int *pin_len) { int err = EXIT_SUCCESS; char ch; fflush(stdout

Reading input from getchar() giving unexpected results

限于喜欢 提交于 2020-05-31 11:43:16
问题 I have a function which reads PIN from terminal and stores PIN and PIN length in the variables passed. On the first call to the function I get expected PIN and PIN length entered. However, during the second call to this function the first character is omitted. /* * Function : read_pin(char *pin,int *pin_len) * Description : Read entered PIN and stores PIN in pin and pin length in pin_len */ int read_pin(unsigned char *pin,unsigned int *pin_len) { int err = EXIT_SUCCESS; char ch; fflush(stdout

STL标准模板库

只愿长相守 提交于 2020-04-08 00:29:32
Vector: 写vector时候碰到这样一段代码: for(int j=0;j<v1.size();j++) { printf("%d %d",j,v1[j]); printf("%d",v1.size()); if(v1[j]==4) { v1.insert(v1.begin(),5); //在访问元素之前插入元素,那么元素的顺序整体就后面移动一位,那么一直访问到的都是4 j++; } getchar(); } Notice: 如果在遍历中需要插入删除元素,那么v1.size不能固定下来,否则会遗漏尾部的元素,在插入之后注意把迭代器或者索引向后移动一位,这样才会指向插入前那个元素的位置。在由迭代器加1指向下一个元素。 其实VECTOR本质上是一个可以容纳任何类型的动态数组。他用到了C++的动态数组,本质上是一个数组,存放在连续的区域,由此可以知道,中间插入或者删除会导致内存区域整块地移动,这样效率很低。所以Vector适合那些经常需要随机访问的类型,当让在末尾push_back,pop_back的速度是很快的,不会导致内存区域的整片移动。 // settest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include<set> #include<algorithm> #include<iterator> using

题型——构造题

﹥>﹥吖頭↗ 提交于 2020-04-06 22:42:06
cf 483C 第一个数设为a[1]=1,后一个数a[2]=a[1]+k,a[3]=a[2]-(k-1),a[4]=a[3]+(k-2),依次构造下去,剩下的数让他们按从小到大依个排下去,不会增加 distinct elements的个数 #include <bits/stdc++.h> #define mp make_pair using namespace std; typedef long long ll; const int maxn = (1<<21)+100; const int INF = 0x3f3f3f3f; const int mod = 998244353; inline int read(){int s=0,w=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();} while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w;} ll qpow(ll p,ll q){return (q&1?p:1)*(q?qpow(p*p%mod,q/2):1)%mod;} int n,m,f,k,i; int main() { n=read(),m=read(); f=k=1; for (i=m;i>=1;i--)