c - 线性搜索与 strlen

标签 c pointers

在我的一项作业中,我需要使用线性搜索来查找字符串的最后一个字符并将其设置为指针,它只是一个简单的字符串,例如“blah blah blah” 。为此,我使用了

int length = strlen(string);

找到长度,然后使用for循环

for (i=1;i<length;i++){
    if (string[i]==0){;
        end_pointer = &string[i-1];
    }

使用线性搜索 0 来设置指针与使用长度设置指针有什么区别:

end_pointer = &string[length-1];

最佳答案

我认为你的教授真正想要的是:

int i = 0;
while( '\0' != string[i] ) i++;

用于搜索

在循环完成后分配以获得最佳效率:

char * end_pointer = &string[i - 1];

关于c - 线性搜索与 strlen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22002575/

相关文章:

c - 在查找最大对和时使用指针

c++ - 不能在数组上使用 .begin() 或 .end()

c - 如何让EUDC在不重启的情况下生效?

c - 在 C 中看起来像三角形的输出

c - C 中两个数组元素地址之间的偏移量

c - 错误 : Assignment makes pointer from Integer without a cast. .. 在 C 程序中

c - time.h - struct tm 和毫秒

C 函数指针

c++ - 存储指向 map 中包含的对象的指针

c++ - 如何在C++中增加指针的指针值