结束字符串标记中的while循环

时间:2014-10-26 09:38:42

标签: c++

如何在getToken

中结束while循环
 int i =0;  //need to be gobal variable because when we use getToken
 next time
            //it should be not zero


char* getToken(const char* text, char end)
{

    char* charPtr = new char[maxWordLength];

    int charCount =0;
    if (text[i] == ' ') {
        i++;
    }
    else if(text[i] == '\0')  //not sure if it's used
        return 0;
    while (text[i] != end)  //content *text point to is not space
    {
        if ('!'<= text[i]&& text[i]<='~') //fromASCII table 33~126 not include space
        {
            charPtr[charCount++]= text[i++];

        }

    }
    charPtr[charCount] = '\0';
    return charPtr;
}

这是来自main()

char input[28] = "hi ok it's me?? no!! ";
cout << getToken(input,' ')<<" ";
cout <<endl;
cout << getToken(input,' ')<<" ";
cout <<endl;
cout << getToken(input,' ')<<" ";
cout << getToken(input,' ')<<" ";
cout << getToken(input,' ')<<" ";

当我把下一个cout&lt;&lt; getToken(输入,'')&lt;&lt;“”;

循环将永远运行......

感谢

//////////////////////添加2014 10 27

如果我想重用struct

中的值
struct subString subStr[NumberOfSubstr];

struct subString
{

    int numberOfSub = 0;
    char data[maxWordLength];

};

在我操作subStr [i]并将数据存储在struct和number中之后 我该如何重用这些数据。

/////////////用于输出令牌////////////////////

 struct GetToken tokenObject;
    struct subString subStr[NumberOfSubstr];
    for (int i=0; i<6; ++i) {
        char *substring2 =subStr[i].data;
        substring2= tokenObject.getToken(ptr, ' ');
        cout <<substring2<<" ";

    }

/////////////////////////////////////////////// //////// 但是在我使用下一个函数之后向下调用struct, 我发现结构中的值都消失了。

for (int i=1; i<6; ++i) {

            char *substring0 = subStr[0].data;
            char *key = keyword;
            if ( easyMatch(substring0, key)>=0) //substring
                subStr[0].numberOfSub++; //for first 1
            char *substring = subStr[i].data;
            int keyLen = size(key);
            int subLen = size(substring);
            if(keyLen<subLen) // not total match maybe substring

            {
                //for second to the last
                //if string j match early string
                //number of early string ++
                //in the end of loop
                //if string is not match but is substring
                //numberof substring ++
                for(int j=i-1;j>=0;j--)
                {
                    char*subBefore =subStr[j].data;
                    char*subNow = subStr[i].data;
                    int subBeforeLen = size(subBefore);
                    int subNowLen = size(subNow);
                if (subBeforeLen == subNowLen &&easyMatch(subBefore, subNow))
                    subStr[j].numberOfSub++;
                }//end for

                char*subNow = subStr[i].data;
                if (easyMatch(subNow, keyword)>0) //new found substring
                    subStr[i].numberOfSub++;

            }//end if
        }//end outer for

如何重用结构中的数据,或者我需要使用另一个数组来保存它

由于结构中的每个字符串都有一个相关的数字,我希望它链接在一起。

非常感谢

2 个答案:

答案 0 :(得分:0)

  

如何结束while循环

break关键字就是这样做的。

答案 1 :(得分:0)

这个循环:

while (text[i] != end)  //content *text point to is not space
{
    if ('!'<= text[i]&& text[i]<='~') //fromASCII table 33~126 not include space
    {
        charPtr[charCount++]= text[i++];

    }

}
当到达输入字符串的末尾时,

不会结束,因为它是一个零值,它超出了您接受的“ascii表”值。因此循环将只是坐在那里旋转,直到你感到无聊并停止它。

解决方案是识别字符串的结尾(或它的长度)。我会这样做:

while (text[i] && text[i] != end)  //content *text point to is not space 
                                   // and not end of string
{
   ... 
}

顺便说一句,你的代码正在泄漏内存,你需要这样做:

tmp = getToken(input, ' ');
cout << tmp << " ";
delete [] tmp;