在C字符串问题 [英] Questions on C strings

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

问题描述

我是新的C,我与C字符串非常困惑。以下是我的问题。

从字符串中查找最后一个字符

我怎样才能找到一个字符串的最后一个字符?我想出了类似的东西,

 的char *海峡=你好;
的printf(%C,STR [strlen的(STR) - 1]);
返回0;

这是要走的路?我莫名其妙地觉得,这并不是因为的strlen 的正确方法有遍历的人物得到的长度。因此,该操作将有一个 O(N)的复杂性。

转换字符的char *

我有一个字符串,需要一个char追加到它。我怎样才能做到这一点? strcat的只接受的char * 。我尝试以下,

 字符分隔符='';
字符文本[6];
的strcpy(文字,你好);
strcat的(文本,分隔符);

strcat的使用具有本地范围变量

请考虑以下code,

 无效美孚(字符*输出)
{
   字符*分隔符='';
   的strcpy(输出,你好);
   strcat的(输出,分隔符);
}

在上面的code,分隔符是被后摧毁了一个局部变量返回。它是确定其追加到变量输出

如何 strcat的处理空终止字符?

如果我连接两个空结尾的字符串,将 strcat的两个空字符终止追加到结果字符串?

是否有一个良好的初级文章,其中介绍了串用C如何工作以及如何执行通常的字符串操作?

任何帮助将是巨大的!


解决方案

  1. 最后一个字符:你的做法是正确的。如果你需要做这个有很多大型的字符串,包含数据结构的字符串应该存储与它们的长度。如果没有,也没关系,它的O(N)。


  2. 追加一个字:你有几个错误。一方面,你的缓冲区太小,无法容纳其他字符。至于如何调用strcat的,你可以把一个字符串中的字符(含2项的数组,第二个是0),或者你可以手动使用长度的写法到底。


  3. 您约2 NUL终结的担心是没有根据的。虽然它占用的内存连续的字符串,是必要的,在最后的NULL字节长度不感字符串的一部分等,这纯粹是结束的标志。 strcat的会覆盖旧NUL,把一个新的在最后,在连接字符串之后。同样,你需要确保你的缓冲区足够大,你打电话之前的strcat!


I am new to C and I am very much confused with the C strings. Following are my questions.

Finding last character from a string

How can I find out the last character from a string? I came with something like,

char *str = "hello";
printf("%c", str[strlen(str) - 1]);
return 0;

Is this the way to go? I somehow think that, this is not the correct way because strlen has to iterate over the characters to get the length. So this operation will have a O(n) complexity.

Converting char to char*

I have a string and need to append a char to it. How can i do that? strcat accepts only char*. I tried the following,

char delimiter = ',';
char text[6];
strcpy(text, "hello");
strcat(text, delimiter);

Using strcat with variables that has local scope

Please consider the following code,

void foo(char *output)
{
   char *delimiter = ',';
   strcpy(output, "hello");
   strcat(output, delimiter);
}

In the above code,delimiter is a local variable which gets destroyed after foo returned. Is it OK to append it to variable output?

How strcat handles null terminating character?

If I am concatenating two null terminated strings, will strcat append two null terminating characters to the resultant string?

Is there a good beginner level article which explains how strings work in C and how can I perform the usual string manipulations?

Any help would be great!

解决方案

  1. Last character: your approach is correct. If you will need to do this a lot on large strings, your data structure containing strings should store lengths with them. If not, it doesn't matter that it's O(n).

  2. Appending a character: you have several bugs. For one thing, your buffer is too small to hold another character. As for how to call strcat, you can either put the character in a string (an array with 2 entries, the second being 0), or you can just manually use the length to write the character to the end.

  3. Your worry about 2 nul terminators is unfounded. While it occupies memory contiguous with the string and is necessary, the nul byte at the end is NOT "part of the string" in the sense of length, etc. It's purely a marker of the end. strcat will overwrite the old nul and put a new one at the very end, after the concatenated string. Again, you need to make sure your buffer is large enough before you call strcat!

这篇关于在C字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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