简体   繁体   English

查找没有C语言中函数的字符串的最后一个字符

[英]Finding last character of a string without a function in C


I want to find the last element of an string without the use of any functions. 我想在不使用任何函数的情况下找到字符串的最后一个元素。
My code seems to work, but I'm not quite sure why it does work in the way it does and would be glad about input. 我的代码似乎可以工作,但是我不太确定为什么它会以这种方式工作,并且会为输入感到高兴。
Here is the code. 这是代码。

#include <stdio.h>

char *find_last(char *s);

int main(void)
{
    char string[] = {"This is an Example"};
    find_last(string);
    return 0;
}

char *find_last(char *s)
{
/*  char last;*/
    while (*s++ != 0) {}
    printf("*s ist %c", *(s-2));
    return 0;
}

I don't understand why I have to print *(s-2) instead of *(s-1) in order to get the last character. 我不明白为什么我必须打印*(s-2)而不是*(s-1)才能获得最后一个字符。 (In order to get a futher character back I only have to decrement one more, ie *(s-3)). (为了获得更多的字符,我只需要再减小一个,即*(s-3))。

Oh, and additionally, it seems that the content of *s seems to be the letter O (from me mistaken as zero, until now), independent of the example-string tested with. 哦,此外,看来* s的内容似乎是字母O(直到现在为止,我对它的理解是零),与测试的示例字符串无关。 Why is that? 这是为什么? I thought it should be zero (0) because I increment the pointer until it hits the binary zero. 我认为它应该为零(0),因为我增加了指针直到它达到二进制零。

Thanks for any help! 谢谢你的帮助!

Since ++ is a post-increment operator, this is what happens in the while loop: 由于++是后递增运算符,因此while循环中会发生以下情况:

  1. *s is compared to 0 . *s0比较。
  2. s is incremented. s增加。
  3. If the comparison succeeds, the loop exits. 如果比较成功,则循环退出。

When s reaches the end of the string it's pointing to the null character. s到达字符串的末尾时,它指向空字符。 It's incremented to point to the character after the null, and then the loop exits. 它递增以指向null 的字符,然后循环退出。 At that point: 在那时候:

*s = the character after the null, which happens to be the letter O in your case. *s =空值后的字符,在您的情况下恰好是字母O

*(s-1) = the null character. *(s-1) =空字符。

*(s-2) = the last character in the string. *(s-2) =字符串中的最后一个字符。

OP later asked about handling a corner case involving "" . OP随后询问有关处理涉及""的极端情况的问题。

First recommend to clarify the goal "to find the last element of an string ". 首先建议明确目标“找到字符串的最后一个元素”。 By definition: in C, 根据定义:在C中,

A string is a contiguous sequence of characters terminated by and including the first null character. 字符串是由第一个空字符终止并包括第一个空字符的连续字符序列。 C11 §7.1.1 1 C11§7.1.11

Since a string includes a null character and that is last, then the "Finding last character of a string" is always the null character . 由于字符串包含一个空字符且该字符最后一个,因此“查找字符串的最后一个字符” 始终空字符

Certainly OP is interested in the last character before the null character . 当然,OP对空字符之前的最后一个字符感兴趣。 Since such a function may receive a valid string "" and not be able to find such a character, then find_last() could error out somehow, or perhaps return an error condition, like NULL below and let the caller decide. 由于此类函数可能会收到有效的字符串 "" ,而无法找到此类字符,因此find_last()可能会以某种方式出错,或者可能返回错误条件(例如下面的NULL ,并让调用者决定。

Starting with OP-like code 从类似OP的代码开始

char *find_last(char *s) {
  printf("Last character before the null character in \"*s\"", s);
  if (*s == '\0') {
    printf(" does not exist.\n");
    return NULL;
  }
  while (*s) {
    s++;
  }
  s--;
  printf(" is '%c'\n", *s);
  return s;
}

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

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