切割'\\ 0'从字符串 [英] Cutting ' \0' from strings

查看:245
本文介绍了切割'\\ 0'从字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从标准例如装载函数getline字符串二维数组:

 大家好,我的名字是约翰。
我喜欢巧克力。

然后我要搜索,如果输入的字符串/子与字符串数组的例子之一匹配:

 翁。 - 在1号线相匹配
巧克力。 - 在2号线相匹配

我使用的是非标准的strstr功能:

 如果((的strstr(数组[我],字符串))!= NULL){
      的printf(匹配);
}

问题是,当我想找到一个字符串,它是不是在字符串的结尾像我写的,它不匹配,因为可能当我想找到喜欢的字符串,它可能比较像\\ 0与象,所以它永远不会匹配。

当我加载使用函数getline行缓冲我使用的功能:strlen的(缓冲)-1,那么我分配的内存函数strlen(缓冲) - 1 *的sizeof(字符),然后将其复制到与memcpy函数的数组。一切正常,但是当字符串已经lenght 7-8它把2未定义字符的字符串例子的末尾:

 输入字符串:testtttt
的memcpy了对strlen(字符串)分配的数组 - 1
从阵列打印字符串看起来像:testttttt1或testtttttqx等。

长度小于7或超过8个字符

弦乐很好地工作。如果你知道如何解决这个问题,或者知道一个更好的方式,从字符串,使\\ 0只是字符串,不\\ 0让我知道我将感谢。

在code不工作的一部分。只有wtith结束串像我mentioned.Pole是一个字符串二维数组匹配,线是那里的字符串存储缓冲区。

 为size_t的len = 0;
字符*线= NULL;
INT编号;
而((数=函数getline(安培;线,&安培;!LEN,标准输入))= - 1){
    对于(i = 0; I<指数;我++){
            如果(的strstr(磁极[I]中,线)!= NULL){
               的printf(匹配);
            }
    }
}
    6
约翰。大家好,我的名字是约翰。
'包含'约翰。

测试的东西
不包含约翰。

我不知道写什么
不包含约翰。

8
测试大家好,我的名字是约翰。
不包含测试

测试的东西
不包含测试

我不知道写什么
不包含测试


知道大家好,我的名字是约翰。
'不包含'知道

测试的东西
'不包含'知道

我不知道写什么
'不包含'知道


解决方案

您的问题是显而易见的在调试输出。 函数getline不会删除从输入换行符,因此,例如您正在搜索:

 知道\\ n

 我不知道该怎么写\\ n

所以你的问题不是关于剥离 \\ 0 字符串结束,而是剥 \\ n 线 - 结束。

这可以以许多方式来实现,例如:

 的char *换行= strrchr(行的'\\ n');
如果(newlineaddr!= NULL)
{
    * newlineaddr ='\\ 0';
}

 为size_t newlineindex = strcspn(行,\\ n);
行[newlineindex] ='\\ 0';

第一COPES多线输入(无需在此情况下) - 只取出最后换行,而第二个是更简洁

I have a 2D array of strings loaded with getline from stdin example :

Hi my name is John.
I like chocolate.

Then i want to search if entered string / substring matches with one of the string arrays example :

Ohn. - matches at line 1
chocolate. - matches at line 2

I'm using standart function strstr :

if ( ( strstr(array[i],string) ) != NULL ) {
      printf("Match");
}

The problem is that when i want to find a string which is not at the end of the string like i wrote , it does not match because probably when i want to find "like" in the string it probably compares like\0 with "like" so it will never match.

When i load the line with getline to buffer i used function: strlen(buffer)-1 then i allocated memory for strlen(buffer) - 1 * sizeof(char) and then copied it to the array with memcpy function. Everything worked perfectly but when the string has lenght of 7-8 it puts 2 undefined characters at the end of the string example :

Enter string :testtttt
memcpy to allocated array of strlen(string) - 1
printed string from array looks like : testttttt1� or testtttttqx etc..

Strings with length less then 7 or more than 8 characters work perfectly. If you know how to fix this problem or know a better way to make from string\0 just string without \0 let me know i will be thankful.

Part of the code which does not work. Only matches wtith ending strings like i mentioned.Pole is 2D array of strings, line is buffer where the string is stored.

size_t len = 0;
char *line = NULL;
int number;
while ( (number = getline(&line, &len, stdin ) ) != -1 ) {
    for (i = 0; i < index; i++) {
            if(strstr(pole[i], line) != NULL) {
               printf("Match");
            }
    }
}


    6 
John.

'Hi my name is John.
' contain 'John.
'
'Testing stuff
' does not contain 'John.
'
'I do not know what to write
' does not contain 'John.
'
8 
Testing

'Hi my name is John.
' does not contain 'Testing
'
'Testing stuff
' does not contain 'Testing
'
'I do not know what to write
' does not contain 'Testing
'
5 
know

'Hi my name is John.
' does not contain 'know
'
'Testing stuff
' does not contain 'know
'
'I do not know what to write
' does not contain 'know
'

解决方案

Your problem is evident in your debug output. getline does not strip the newline from the input, so for example you are searching for:

"know\n" 

in

"I do not know what to write\n"

So your problem is not about stripping the \0 string terminator, but rather stripping the \n line-end.

That can be achieved in a number of ways, for example:

char* newline = strrchr( line, '\n' ) ;
if( newlineaddr != NULL )
{
    *newlineaddr  = '\0' ;
}

or

size_t newlineindex = strcspn(line, "\n") ;
line[newlineindex] = '\0' ;

The first copes with multi-line input (not needed in this case) - only removing the last newline, while the second is more succinct.

这篇关于切割'\\ 0'从字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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