简体   繁体   English

c打印字符串语法

[英]c printing string syntax

Just stuck on c syntax regarding strings. 只是停留在有关字符串的C语法上。

Say I have a string like ( name[5]="peter"; ) in c say if I just wanted to print the last character of string or check the last character of the string, which in this case would be 'r' how can I do this? 假设我在c中有一个类似( name[5]="peter"; )的字符串,如果我只是想打印字符串的最后一个字符或检查字符串的最后一个字符,在这种情况下将是'r'我可以这样做吗?

The way I was thinking does not seem to work 我的想法似乎不起作用

name[5]="peter";
if(name[5]=="r") printf("last character of name is r");

Question: is there some sort of function to do this that can check one character of array, is a certain value, like name[5] is 'r' in string peter or likewise name[1] is 'n' 问题:是否有某种函数可以检查数组的一个字符,是否为某个值,例如字符串peter中的name [5]为'r'或同样的name [1]为'n'

Also how do I use printf to print that certain char, having problems using 还有我如何使用printf打印某些字符,使用时遇到问题

printf("last character of name is %s",name[5]) ???

Thanks 谢谢

First thing, strings are null-terminated. 首先,字符串以空值结尾。 For a five-character string you need to allocate a 6-character array to handle the '\\0' character at the end of the string. 对于5个字符的字符串,您需要分配6个字符的数组以处理字符串末尾的'\\0'字符。

char name[6] = "peter";
// peter is {'p', 'e', 't', 'e', 'r', '\0'}

To check what individual characters are, index the string using square brackets. 要检查什么字符,请使用方括号将字符串索引。 The first character is index 0, the second is index 1, etc. Also, C makes a distinction between strings and individual characters. 第一个字符为索引0,第二个为索引1,依此类推。此外,C区分字符串和单个字符。 A string is written with "double quotes" . 字符串写有"double quotes" Characters are written with single quotes: 'r' . 字符用单引号'r'书写。

if (name[4] == 'r') {
    printf("fifth character of name is r\n");
}

To find the last character you need to know the length of the string. 要找到最后一个字符,您需要知道字符串的长度。 If you know the length ahead of time you can hard code it; 如果您提前知道长度,则可以对其进行硬编码。 otherwise, use the strlen function to calculate the string length. 否则,使用strlen函数计算字符串长度。 And then subtract 1 because indexes are 0-based. 然后减去1,因为索引是从0开始的。

if (name[strlen(name) - 1] == 'r') {
    printf("last  character of name is r\n");
}

To print individual characters with printf you can use the %c format specifier. 要使用printf打印单个字符,可以使用%c格式说明符。 %c prints a single character. %c打印一个字符。

printf("fifth character of name is %c\n", name[4]);
printf("last  character of name is %c\n", name[strlen(name) - 1]);

I think you want this: 我想你想要这个:

char *s = "peter";
char lastChar = s[strlen(s) - 1];
printf("last character in s is %c\n", lastChar);

Your problem is that strings in C are null-terminated. 您的问题是C中的字符串以null结尾。 That means that they end in the '\\0' character. 这意味着它们以'\\0'字符结尾。 You have to save space for that. 您必须为此节省空间。 Moreover, arrays are indexed from 0 , so name[5] specifies a string with 4 characters and a null character, with the null character at index 4 of name . 此外,数组从0索引,因此name[5]指定一个包含4个字符和一个空字符的字符串,该空字符位于name索引4处。

You need to fix these: 您需要解决以下问题:

char name[6]="peter";

so now: 所以现在:

name[0] = 'p'
name[1] = 'e'
...
name[4] = 'r'
name[5] = 0

So your if statement should use index 4, not 5: 因此,您的if语句应使用索引4,而不是5:

if(name[4]=="r") printf("last character of name is r");

Finally, to print a single char, use th %c modifier, not %s . 最后,要打印单个字符,请使用%c修饰符,而不是%s Your final printf should read: 您的最终printf应该为:

printf("last character of name is %c",name[4]) printf(“名称的最后一个字符为%c”,名称[4])

Otherwise you're printing a string that begins from the offset you specified, which (if you allocated enough space in your string) will be '\\0' , which is an empty string. 否则,您将从指定的偏移量开始打印一个字符串,该偏移量(如果在字符串中分配了足够的空间)将为'\\0' ,这是一个空字符串。

There are two big issues here: 这里有两个大问题:

  • First is the length of the string 首先是字符串的长度
  • Second is the index of the string. 第二个是字符串的索引

Length 长度

In C, strings are NULL-terminated, meaning they have an invisible \\0 at the end to mark the end of the string. 在C中,字符串以NULL终止,这意味着它们的末尾有一个不可见的\\0来标记字符串的结尾。 You have to account for this invisible charater, so while peter is only 5 letters, you need one more space for the Null-terminator. 您必须考虑到这种无形的字符,因此,尽管peter只有5个字母,但您需要为Null终止符再加上一个空间。

char name[6]="peter";  /* implied \0 at the end of the string */

Index 指数

In C, strings and arrays are 0-indexed, meaning that the first element is [0]. 在C语言中,字符串和数组的索引为0,这意味着第一个元素为[0]。 In the case of your string, the indexes are: 对于您的字符串,索引为:

[0] `p`
[1] `e`
[2] `t`
[3] `e`
[4] `r`
[5] `\0`

So where you expect r to be the 5th character, it is at index 4 . 因此,在您期望r为第5个字符的情况下,它位于索引4处

If you declare the string properly (with 6 elements instead of 5), and look at index [4], you'll find that is is r as expected. 如果正确声明字符串(使用6个元素而不是5个元素),并查看索引[4],则将发现r为预期值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM