C 打印字符串数组
我正在分析一个文本文件:C 打印字符串数组,c,arrays,parsing,C,Arrays,Parsing,我正在分析一个文本文件: Hello, this is a text file. 并通过将文件转换为char[]来创建。现在,我想获取数组,遍历它,并创建一个数组数组,将文件拆分为单词: string[0] = Hello string[1] = this string[2] = is 这是我的代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include "TextRe
Hello, this is a text file.
并通过将文件转换为char[]来创建。现在,我想获取数组,遍历它,并创建一个数组数组,将文件拆分为单词:
string[0] = Hello
string[1] = this
string[2] = is
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "TextReader.h"
#include <ctype.h>
void printWord(char *string) {
int i;
for (i = 0; i < strlen(string); i ++)
printf("%c", string[i]);
printf("\n");
}
void getWord(char *string) {
char sentences[5][4];
int i;
int letter_counter = 0;
int word_counter = 0;
for (i = 0; i < strlen(string); i ++) {
// Checks if the character is a letter
if (isalpha(string[i])) {
sentences[word_counter][letter_counter] = string[i];
letter_counter++;
} else {
sentences[word_counter][letter_counter + 1] = '\0';
word_counter++;
letter_counter = 0;
}
}
// This is the code to see what it returns:
i = 0;
for (i; i < 5; i ++) {
int a = 0;
for (a; a < 4; a++) {
printf("%c", sentences[i][a]);
}
printf("\n");
}
}
int main() {
// This just returns the character array. No errors or problems here.
char *string = readFile("test.txt");
getWord(string);
return 0;
}
我怀疑这与指针之类的东西有关。我有很强的Java背景,所以我仍然习惯C。使用
句子[5][4]
您将句子的数量限制为5个,每个单词的长度限制为4个。为了处理更多更长的单词,您需要将其变大。试试句子[10][10]
。您也没有检查您输入的单词是否不超过句子所能处理的长度。对于较大的输入,这可能导致堆溢出和访问冲突,请记住C不会为您检查指针
当然,如果你想用这种方法处理更大的文件和更大的单词,你需要把它做得更大。使用句子[5][4]
你将句子的数量限制在5个,每个单词的长度限制在4个。为了处理更多更长的单词,您需要将其变大。试试句子[10][10]
。您也没有检查您输入的单词是否不超过句子所能处理的长度。对于较大的输入,这可能导致堆溢出和访问冲突,请记住C不会为您检查指针
当然,如果要将此方法用于包含较大单词的较大文件,则需要将其变大。不使用strok的示例:
void getWord(char *string){
char buff[32];
int letter_counter = 0;
int word_counter = 0;
int i=0;
char ch;
while(!isalpha(string[i]))++i;//skip
while(ch=string[i]){
if(isalpha(ch)){
buff[letter_counter++] = ch;
++i;
} else {
buff[letter_counter] = '\0';
printf("string[%d] = %s\n", word_counter++, buff);//copy to dynamic allocate array
letter_counter = 0;
while(string[++i] && !isalpha(string[i]));//skip
}
}
}
使用strtok版本:
void getWord(const char *string){
char buff[1024];//Unnecessary if possible change
char *p;
int word_counter = 0;
strcpy(buff, string);
for(p=buff;NULL!=(p=strtok(p, " ,."));p=NULL){//delimiter != (not isaplha(ch))
printf("string[%d] = %s\n", word_counter++, p);//copy to dynamic allocate array
}
}
不使用strtok的示例:
void getWord(char *string){
char buff[32];
int letter_counter = 0;
int word_counter = 0;
int i=0;
char ch;
while(!isalpha(string[i]))++i;//skip
while(ch=string[i]){
if(isalpha(ch)){
buff[letter_counter++] = ch;
++i;
} else {
buff[letter_counter] = '\0';
printf("string[%d] = %s\n", word_counter++, buff);//copy to dynamic allocate array
letter_counter = 0;
while(string[++i] && !isalpha(string[i]));//skip
}
}
}
使用strtok版本:
void getWord(const char *string){
char buff[1024];//Unnecessary if possible change
char *p;
int word_counter = 0;
strcpy(buff, string);
for(p=buff;NULL!=(p=strtok(p, " ,."));p=NULL){//delimiter != (not isaplha(ch))
printf("string[%d] = %s\n", word_counter++, p);//copy to dynamic allocate array
}
}
看看strtok——它已经根据提供的分隔符将char*字符串拆分为更小的部分。看这里:这个答案正是我想要的答案。然而,有些语法令人困惑。尽管如此,它确实做到了我想要的。看看strtok——它已经根据提供的分隔符将char*字符串拆分为更小的部分。看这里:这个答案正是我想要的答案。然而,有些语法令人困惑。尽管如此,这正是我想要的。