gpt4 book ai didi

c - 如何将 char * 转换为 2d char 数组

转载 作者:太空宇宙 更新时间:2023-11-04 02:47:13 26 4
gpt4 key购买 nike

我正在尝试获取一个 char *,它代表一个单词,我从一个函数中获取了 char *,并将其放入一个二维数组中,但取而代之的是它只是多次重复每个单词的第一个字母。输出看起来像这样

ttttttttttttttttttttttttttttttttvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvoooooooooooooooooooooooooo

我的输入文件是

three
versions
of
simple
with
spell
check
checking
program
we
will
write

我不确定如何正确地将 char * 转换为二维数组这是我正在使用的所有功能

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include "dict.h"



bool is_valid_entry( int strLength, char entry[] ){

if( entry[0] != '"' && !isalpha(entry[0]) && entry[0] != '\'' ){
return false;
}

strLength--; /* to allow for the zero index of the array*/

for ( int i=1; i < strLength; i++ )
if ( !isalpha( entry[i] ) ){

return false;
}

if (!isalpha( entry[strLength] ) && !ispunct( entry[strLength] ) && entry[strLength] != '"' && entry[strLength] != '\'' ){

return false;
}
return true;/* if the entry passes all of the tests the function will return true meaning the the word is valid*/

}/* ends is_valid_entry( int strlength, char entry[] )*/



char * normalize( int strLength, char entry[], char output[] ){

strLength--;/* to allow for the zero index of an array*/
int j = 0;

for( int i = 0;i < strLength; i++){/* converts all of the elements in the entry[] to lower case*/
if (isupper( entry[i] ) ){
entry[i] = tolower( entry[i] );
}
}

if( ( entry[0] == '"' && entry[strLength] == '"' ) || (entry[0] == '\'' && entry[strLength] == '\'' ) ) {
for(int i=1 ; i < strLength ; i++,j++ ){
output[j] = entry[i];
}
output[j] = '\0';/* removes the last character which is either a '"' ir a '\''*/
return output;/* returns the noramlized word*/

}else if( entry[0] == '"' || entry[0] == '\'' ){
for(int i = 1; j < strLength; i++, j++ ){/*i=1 in order to skip the first element in the entry arrary*/
output[j] = entry[i];
}
output[j] = '\0';
return output;/* returns the noramlized word*/

} else if( entry[strLength] == '"' || ispunct( entry[strLength] ) || entry[strLength] == '\''){
for(int i = 0;j < strLength; i++,j++ ){
output[j] = entry[i];
}
output[j] = '\0';
return output;/* returns the noramlized word*/
}


return entry;/* returns the original array since it does not need to be normalized*/

}/* ends normalize( int strlength, char entry[], char output[] )*/
void load_dict(char *fileName, char dictionary[30000][31]) {

FILE *fdict;
fdict = fopen( fileName, "r" );/* file pointer for the dictionary file*/
char *normDictWord;
char normDict [33];

int strLength,i,j,ch;
if ( fdict == NULL ){
fprintf(stderr,"Could not open file: %s\n", fileName );/*checks to make sure the dictionary file can be opened*/
exit(1);
}

for (i = 0; (ch = fgetc( fdict ) ) != EOF; i++ ) {

char word[33] = "";/* resets the array*/


for (strLength = 0; !isspace( ch ) ; strLength++ ){
word[strLength] = ch;
ch = fgetc( fdict );
}

if (is_valid_entry( strLength , word ) ){
normDictWord = normalize( strLength , word , normDict );/*normalize then do the linear search then print the word if it is not found in the dictionary*/

for(j = 0; j <= 31;j++){
dictionary[i][j] = * normDictWord ;
printf("%c",dictionary[i][j]);
}

}


}
fclose( fdict );
}

最佳答案

我不能完全理解你的代码,修复格式,并添加一些缺失的部分(否则我无法测试它)。然而:

dictionary[i][j] = * normDictWord;

应该是这样的:

dictionary[i][j] = * (normDictWord + j);

或等同于:

dictionary[i][j] = normDictWord[j] ;

关于c - 如何将 char * 转换为 2d char 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26028571/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com