Home > Back-end >  How to input one character at a time, in turn in c language and continuous input two carriage return
How to input one character at a time, in turn in c language and continuous input two carriage return

Time:09-29

CodePudding user response:

Enter a character with getchar (), please input it is ok to use cycle;
Want to by two press enter to end the input, could be obtained with two variables, jump out of the loop when two variables at the same time to enter
input a block of codeFor example,
 
='a' char ch1 and ch2='a';
While (ch1=='\ n' & amp; & Ch2=='\ n')
{
Ch1=getchar ();
If (ch1=='\ n' & amp; & Ch2! )
='\ n'{
Ch2=ch1;
Ch1='a';
}
If (ch1=='\ n' & amp; & Ch2=='\ n')
break;
The else
Ch2='a';
}

CodePudding user response:

 # include & lt; stdio.h> 

# define STR_LEN 128

Int main (void)
{
Char STR [STR_LEN];
Int ch1 and ch2=0, I;

I=0;
While (1) {
//printf (" do input NO. % d character! \ n ", I + 1);
Ch1=getchar ();
If (ch1==10 & amp; & Ch2==10)//'\ n'==10
break;
If (ch1!=10 & amp; & i STR [I]=ch1;
If (I & gt;=STR_LEN)
break;
i++;
Ch2=ch1;
}



return 0;
}

For your reference ~

CodePudding user response:

 # include & lt; stdio.h> 

# define STR_LEN 128

Int main (void)
{
Char STR [STR_LEN];
Int ch1 and ch2=0, I, j;

I=0, j=1;
While (1) {
If (j & gt; I)
Printf (" input NO. % d character! \ n ", I + 1);
Ch1=getchar ();
If (ch1==10 & amp; & Ch2==10)//'\ n'==10
break;
If (ch1!=10 & amp; & i STR [I]=ch1;
i++;
J=I + 1;
}
If (ch1==10)
J=I;
If (I & gt;=STR_LEN)
break;
Ch2=ch1;
}



return 0;
}

For your reference ~

Recommended to this code, the above is a little problem ~
  • Related