20分代码,命令行会吞掉一个空格,求助!!!
查看原帖
20分代码,命令行会吞掉一个空格,求助!!!
757040
MuLinnnnn楼主2022/11/9 21:18
#include <iostream>
#include <cstring>
using namespace std;

int main() {
	char word[15],sentwd[30];
	int time=0,sum=0,ind=0,space=-1;
	int i=0;
	cin>>word;
	for(i=0;i<int(strlen(word));i++){
	    word[i]=toupper(word[i]);
    }
	getchar();
	while((sentwd[time] = toupper(getchar()))) {
	    cout << sentwd[time] << "<-sentwd[time]\n";
        char ch=sentwd[time];
		ind++;
		time++;
		if(isspace(sentwd[time-1])||sentwd[time-1]=='\n') {
			sentwd[time-1]='\0';
//			cout << sentwd << "<-sentwd\n";
			if(!strcmp(sentwd,word)) {
				sum++;
				if(space<0) {
					space=ind-int(strlen(sentwd))-1;
//					cout << space <<"<-space\n";
				}
			}
//			cout << time-1 << "<-time-1\n";
			if(ch=='\n') {
//			    cout << "go\n"; 
				break;
			}
			time=0;
		}
	}
	if(space<0){
	    cout << -1;
    }else{
        cout << sum << ' ' << space << "\n";
    }
	return 0;
}
2022/11/9 21:18
加载中...