#include
#define MAXIMUM 1000 //這段代碼是要實現將一個字符串中的連續的空格用數
int getline(char line[]); //較少的指標符和空格代替,請指點,如其中有什麼低等
main(){ //錯誤也請大神們指點,本人新手只懂皮毛,謝謝。
int len;
char line[MAXIMUM];
char newline[MAXIMUM];
int j;
int k = 0;
int ecount;
int n;
while((len = getline(line) > 0)){
for(j = 0;j < len;j++){
if(line[j] != ' ')
newline[k++] = line[j];
else if(line[j] == ' '){
while(line[j++] == ' '){
ecount++;
}
if(ecount == 1){
newline[k++] = ' ';
}else if(ecount == 2){
newline[k++] = ' ';
newline[k++] = ' ';
}else if(ecount >= 3){
for(n = 0;n <(ecount - (ecount%3))/3;n++){
newline[k++] = '\t';
}
for(n = 0;n < (ecount%3);n++){
newline[k++] = ' ';
}
}
newline[k++] = line[j];
}
}
}
printf("%s\n",newline);
return 0;
}
int getline(char line[]){
int i;
int c;
for(i = 0;(c = getchar()) != EOF&&c != '\n';i++)
line[i] = c;
if(c = '\n'){
line[i] = c;
i++;
}
line[i] = '\0';
return i;
}
錯誤太多,排版啦、== 用錯成 = 啦、循環計數各種亂啦,回去再學學吧
#include <stdio.h>
#define MAXIMUM 1000
int getline(char * line);
int main(){
int len;
char line[MAXIMUM];
char newline[MAXIMUM];
while((len = getline(line)) > 0){
int j = 0;
int k = 0;
printf("src[%d]:%s\n", len, line);
for(j = 0; j < len; ){
if(line[j] != ' ') newline[k++] = line[j++];
else {
int ecount = 0;
int i = j;
while(line[i++] == ' ')ecount++;
j += ecount;
int tab_count = ecount / 3;
int space_count = ecount - tab_count * 3;
int n;
//你用tab和空格調試的時候能看見嗎???
for(n = 0; n < tab_count; n++)newline[k++] = '#';
for(n = 0; n < space_count; n++)newline[k++] = '_';
}
}
//newline[len] = '\0';
printf("k:%d %s\n",k, newline);
}
return 0;
}
int getline(char * line){
int i, c;
for(i = 0; (c = getchar()) != EOF && c != '\n'; i++) line[i] = c;
if(line[0] == '\0')return 0;
line[i] = '\0';
return i;
}