This is the program where I want to input a string with scanf()
using '%c'
as a format specifier and may use any loop or condition.
// Online C compiler to run C program online
#include <stdio.h>
#include <stdlib.h>
void clrr(){while (getchar()!='\0');}
int main() {
// Write C code here
char str[5];
int i=0;
char a;
while (i<5){
scanf("%1c",str[i]);
a=getchar();
if (a=='\n') {
break;
};
i++;
str[i]=a;
i++;
//clrr();
}
//str[i+1]='\0';
printf("\n%s",str);
return 0;
}
try this...
#include <stdio.h>
#include <stdlib.h>
void clrr(){while (getchar()!='\0');}
int main() {
// Write C code here
char str[5];
int i;
for(i=0;i<5;i++){
scanf(" %c",&str[i]);
}
for(i=0;i<5;i++){
printf("%c",str[i]);
}
return 0;
}
// Online C compiler to run C program online
#include <stdio.h>
int main() {
// Write C code here
char str[5];
int i=0;
for(i=0;(i<5)&&((i>0)?(str[i-1]!='\n'):(1));i++){
scanf("%1c",&str[i]);
}
str[i-1]='\0';
printf("\n%s",str);
return 0;
}
I guess this is the solution... by InZamam
'''// Online C compiler to run C program online
#include <stdio.h>
int main() {
// Write C code here
char str[5];
int i=0;
for(i=0;(i<5)&&((i>0)?(str[i-1]!='\n'):(1));i++){
scanf("%1c",&str[i]);
}
str[(str[i-1]!='\n')?(i):(i-1)]='\0';
printf("\n%s",str);
printf("..");
return 0;
}
'''
A little modified due to preservation of data:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.