首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

char数组 第23个起 读入错误

2012-06-13 
char数组 第23个起 读入异常C/C++ code////main.c//wordCount////Created by Dave Mark on 7/4/11.//Copyr

char数组 第23个起 读入异常

C/C++ code
////  main.c//  wordCount////  Created by Dave Mark on 7/4/11.//  Copyright 2011 Dave Mark. All rights reserved.//#include <stdio.h>#include <ctype.h> //This is to bring in the declaration of isspace()#include <stdbool.h>  //This is to bring in the define of true#define kMaxLineLength        200#define kZeroByte            0void    ReadLine( char *line );int        CountWords( char *line );int main (int argc, const char * argv[]) {    char    line[ kMaxLineLength ];    int        numWords;        printf( "Type a line of text, please:\n" );        ReadLine( line );    numWords = CountWords( line );        printf( "\n---- This line has %d word", numWords );        if ( numWords != 1 )        printf( "s" );        printf( " ----\n%s\n", line );        return 0;}void    ReadLine( char *line ) {    int     numCharsRead = 0;        while ( (*line = getchar()) != '\n' ) {        line++;        if ( ++numCharsRead >= kMaxLineLength-1 )            break;    }        *line = kZeroByte;}int    CountWords( char *line ) {    int        numWords, inWord;        numWords = 0;    inWord = false;        while ( *line != kZeroByte ) {        if ( ! isspace( *line ) ) {            if ( ! inWord ) {                numWords++;                inWord = true;            }        }        else            inWord = false;                line++;    }        return numWords;}




l后面不知道为啥出来\xe0\x7fGj...等的一堆,之前22个,都只是一个字母而已。 不知道有木大侠知道这是啥情况。


[解决办法]
应该不会啊,楼主是输入了什么字符串,我把 line输出来,是对的啊。。。
[解决办法]
没问题呀。

热点排行