与指针运算问题 - 尝试来标记输入字符串 [英] Issues with Pointer Arithmetic - Trying to tokenize input String

查看:146
本文介绍了与指针运算问题 - 尝试来标记输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我工作的一个程序,允许用户通过使用指针数组来输入一个字符串,然后将其标记化,那么令牌显示在屏幕上。这是应该通过调用我的记号化功能,读取输入字符串直到第一个分离器要做到这一点('','','','?','!')。然后,它改变了我的字符串为NULL字符的分隔符。然后,它应该在我的字符串返回一个指向下一个字符。
在主字符串已经被输入后,就应该保持通话,返回指针然后将它们存储指针的数组中后期打印我的令牌标记化功能。一旦标记化()返回一个指针,这是我的字符串的结尾是从环打破了NULL字符。然后,我打印出来的标记使用我的指针数组。
//试图要详细

Currently I am working on a program that allows a user to enter a string that is then tokenized, then the tokens are printed to the screen by using an array of pointers. It is "supposed" to do this by calling my tokenize function which reads the input string until the first separator ( ' ', ',', '.', '?', '!'). It then changes that separator in my string to a NULL char. It then should return a pointer to the next character in my string. In main after the string has been input, it should keep calling the tokenize function which returns pointers which are then stored in a array of pointers to later print my tokens. Once the tokenize() returns a pointer to a NULL character which is at the end of my string it breaks from that loop. Then I print the tokens out using my array of pointers. //trying to be detailed

#include <stdio.h>
#include <string.h>

char *tokenize ( char *text, const char *separators );

int main ( void )
{
        char text[30];
        char separators[6] = { ' ','.',',','?','!','\0'};
        char *pch = NULL;
        int tokens[15];
        int i = 0;
        int j = 0;

        printf("Enter a string: \n");
        fgets( text, 30, stdin );

        printf("%s", text );

        pch = tokenize ( text, separators );

        do
        {
                pch = tokenize ( pch, separators );
                //printf("%c", *pch);
                tokens[i] = pch;
                i++;
        }
        while( *pch != NULL );


        i--;
        while( j != i )
        {
                printf("%s", tokens[i] );
                j++;
        }

        return 0;
}

char *tokenize ( char *text, const char *separators )
{
        while( text != NULL )
        {

                if( text != NULL )
                {
                        while( separators != NULL )
                        {
                                if( text == separators )
                                {
                                        text = '\0';
                                }
                                separators++;
                        }
                }
                text++;
        }
        return text;


}

3大知名目前的问题。
1.当我编译时,它读取字符串然后打印,然后卡在什么也没有打印的无限循环,仍试图获取输入。
2.林pretty知道我现在用的是*我错了地方的指针。
3.我的函数传递到我的数组的引用,所以我认为我可能只是增加他们原样。

3 big known problems currently. 1.When I compile, it reads the string then prints it, then gets stuck in a endless loop with nothing printing, still trying to get input. 2. Im pretty sure I am using the " * " for my pointers in the wrong place. 3. My function passes in a reference to my arrays, so I assumed i could just increment them as is.

我AP preciate任何反馈!我将不断关注这个帖子。如果我离开的东西不清楚,我可以respecify。谢谢你。

I appreciate any feedback! I will be watching this post constantly. If i left something unclear, I can respecify. Thanks.

推荐答案

您有正确的想法接近的问题,但您在整个$有许多指针/ INT 错误C $℃。请确保您编译code启用 警告,这将告诉你,你有你的code问题。 (不要指望,直到你解决您的code正确运行,消除所有的警告)。至少,编译 -Wall -Wextra 选项构建命令。

You had right idea for approaching the problem, but you had numerous pointer/int errors throughout your code. Make sure you compile your code with Warnings enabled, this will tell you where you have problems in your code. (don't expect your code to run correctly until you address and eliminate all warnings). At a minimum, compile with -Wall -Wextra options in your build command.

有一个更容易的方式来做到这一点,但对于学习经验,这是一个很好的锻炼。下面是你的code与纠正的错误。如果可能的话,我已经离开你原来的code 评论,所以你能看到的问题是。我还包括一个位code的去除换行在<$ C月底包括与fgets $ C>文本。虽然这不是必须的,这是很好的做法不具有杂散换行通过您的code滤波器。

There are a lot easier ways to do this, but for the learning experience, this is a great exercise. Below is your code with the errors corrected. Where possible, I have left your original code commented so you can see where the issues were. I also include a bit of code to remove the newline included by fgets at the end of text. While this isn't required, it is good practice not to have stray newlines filter through your code.

让我知道如果您有任何疑问:

Let me know if you have questions:

#include <stdio.h>
#include <string.h>

char *tokenize ( char *text, const char *separators );

int main ( void )
{
        char text[30];
        char separators[6] = { ' ','.',',','?','!','\0'};
        char *pch = NULL;
        char *tokens[15] = {0};             /* declare array of pointers    */
        int i = 0;
        int j = 0;

        printf("Enter a string: \n");
        fgets( text, 30, stdin );
        size_t len = strlen (text);

        if (text[len-1] == '\n')            /* strip newline from text      */
            text[--len] = 0;

        pch = text;                         /* pch pointer to next string   */
        char *str = text;                   /* str pointer to current       */

        do
        {
                pch = tokenize ( str, separators ); /* pch points to next   */
                tokens[i++] = str;                  /* save ptr to token    */
                str = pch;                          /* new start of str     */
        }
        while (pch != NULL && *pch != 0);           /* test both pch & *pch */

        printf ("\nTokens collected:\n\n");
        while (tokens[j])                   /* print each token             */
        {
                printf("  token[%d]: %s\n", j, tokens[j] );
                j++;
        }
        printf ("\n");

        return 0;
}

char *tokenize ( char *text, const char *separators )
{
        const char *s = separators;     /* must use pointer to allow reset  */

        //while( text != NULL )
        while( *text != '\0' )
        {
                s = separators;         /* reset s                          */
                while( *s != 0 )        /* 0 is the same as '\0'            */
                {
                        //if( text == separators )
                        if( *text == *s )
                        {
                                //text = '\0';
                                *text = '\0';
                                return ++text;
                        }
                        s++;
                }
                text++;
        }
        return text;
}

示例输出:

$ ./bin/tokenizestr
Enter a string:
This is a test string

Tokens collected:

  token[0]: This
  token[1]: is
  token[2]: a
  token[3]: test
  token[4]: string

这篇关于与指针运算问题 - 尝试来标记输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆