C,找到​​角色的索引

时间:2017-04-13 18:49:53

标签: c

好吧,我要做的就是找到单词第一个字符的索引。在这里,一个单词只包含字母表而不是其他内容。

现在,我将fgetc或ftell()指针放到它必须的位置时遇到了麻烦。

例如,在任意字符串“A Greatest America,2014 is the”中, 它会找到正确的

指数
'A': 0, 
'Greatest': 2, 
'America': 11,

但是对于'是',这是下一个单词,它将显示18(正确的是25)。 然后在单词'the'之后,它将再次显示它的正确索引。

我知道问题是我的nextword()方法在遇到空间时返回并且只要它碰到一个就设置了wordPos,但是我无法找到一种方法来识别像2014年那样的“not-words”并转移到下一个正确的词。

这是我写的代码。

#define MAXWORD 200
char word[MAXWORD];
int wordLength;
int wordCount;
int charCount;
int wordPos;

// A word is a sequence of alphabetical characters.
static char * nextword(FILE * fd) {
    unsigned int c;
    wordLength = 0;

    while ((c = fgetc(fd)) != -1){      
        if (c != EOF && c != ' ' && c != '\n' && c != '\t' && c != '\r'){
            if (isalpha(c)) 
                word[wordLength++] = c;
        }
        else{
            word[wordLength] = '\0';

            if (wordLength == 0)
                continue;       
            return word;
        }
    }
    if (wordLength > 0){
        word[wordLength] = '\0';

        return word;
    }
    else{
        return NULL;
    }

}

// Conver string to lower case
void toLower(char *s) {
    for (int i = 0; s[i]; i++){
        s[i] = tolower(s[i]);
    }
}


// Read a file and obtain words and positions of the words and save them in table.
int wtable_createFromFile(WordTable * wtable, char * fileName, int verbose)
{
    char *w;
    int i = 0;
    FILE *fd = fopen(fileName, "r");
    if (fd != NULL){
        wordPos = 0;
        while ((w = nextword(fd)) != NULL){
            toLower(w);
            wtable_add(wtable, w, wordPos);
            if(verbose)
                printf("%d: word=%s, pos=%d\n", i++, w, wordPos);
            wordPos = ftell(fd);
        }
    }   
    return 0;   
}

2 个答案:

答案 0 :(得分:1)

由于您正在寻找单词边界而单词仅由字母组成,因此您真正需要检查的唯一内容是某些内容是否为字母。不要检查空白,而是将所有视为与空白完全不同的字符。您应该能够将nextword函数简化为:

int c;
wordLength = 0;

while ((c = fgetc(fd)) != EOF)
{
    if (isalpha(c))
    {
        // Letters get added to the buffer
        word[wordLength++] = tolower(c);
    }
    else
    {
        // A non-letter signifies the end of the current
        //  word, unless we haven't seen any letters yet.
        if (wordLength > 0)
            break;
    }
}

if (wordLength == 0)
    return NULL;

// Terminate string and return
word[wordLength] = '\0';
return word;

我无法对此进行测试,因为您发布的样本不足以编译,但希望它会让您朝着正确的方向前进。

答案 1 :(得分:0)

在只有一个if-then-else的循环内部将覆盖所有情况:

public interface IUserImmediateRepository
{
    void UpdateName(Int32 id, String newName);
}

public class UserImmediateRepository : IUserImmediateRepository
{
    public UserImmediateRepository()
    {
    }

    public void UpdateName(Int32 id, String newName)
    {
        using(var singleUseContext = new MyDbContext())
        {
            var repo = new UserRepository(singleUseContext);
            repo.UpdateName(id, newName);
            singleUseContext.SaveChanges();
        }
    }
}

这可确保wordPos以非字母进行。

使用still ftell或

if [ test ]
then
  do something
else
  do something else
fi  

更好的代码是可以想象的,所以让我们等待其他答案。