String input, with the cyclic loading can be done scanf

Under normal circumstances the input character string is input to an array storage.

100 such as input a string length often do
#include<bits/stdc++.h>
using namespace std;
int main(){
	char a[100];
	for(int i=0;i<100;i++){
		cin>>a[i];
	}
	return 0;
} 

If you do not use string and STL,
using only char array to store it, it is there a lot can waste a lot of space and save any length how to do it, such as your string length is 50, then the additional space 50 is wasted to no avail to, and if you want to enter the string length is 200, then another 100 can not be deposited.

So how can we not use the array, the input string it!

I am a boy example input string, can be used while adding a single character-by-input scanf
#include<bits/stdc++.h>
using namespace std;
int main(){
	char a;
	while(scanf("%c",&a),a!='.'){
		cout<<a;
	}
	return 0;
} 

Here Insert Picture Description

Here a brief mention of another powerful scanf, scanf can enter several characters in the run box, after a character behind the operation in front of the character, and then enter from this side scanf has some saving.

So even does not know the length of the string can be entered, but will not be wasted and an array of cross-border issues.

Released eight original articles · won praise 8 · views 207

Guess you like

Origin blog.csdn.net/xzy15703841578/article/details/105249581