Home>

I want to determine if the last character is a specific character.
Its specific character is ".".
As you can see from the contents of the assert in the source code, the end of the address is "23." and the address notation is incorrect, so I want to make this an error.
But
if (strstr (src, "23.")) {
return (0);
Only the source code like
The created source code works like inet_pton.
I would appreciate your help.
Thank you in advance.

Applicable source code
#include 
#include 
#include<stdio.h>
#include<string.h>
int inet_pton (const char * src, void * dst) {
    // variable declaration
    int suuti1, suuti2, suuti3, suuti4;
    // union type definition
    union number {
        uint32_t number1;
        uint8_t number2 [4];
    };
    // Declaring union variable names
    union number No;
    // Convert the string of src to a numeric value and determine if there are four
    if (sscanf (src, "% d.% d.% d.% d",&suuti1,&suuti2,&suuti3,&suuti4)! = 4) {
       return (0);
    }
    // Store the value converted to numeric value in a variable
    No.number2 [0] = suuti1;
    No.number2 [1] = suuti2;
    No.number2 [2] = suuti3;
    No.number2 [3] = suuti4;
    // copy number1 memory to dst
    memcpy (dst,&No.number1, 4);
    return (1);
}
int main () {
    int result;
    struct in_addr in_addr;
    result = inet_pton ("130.0.7.23",&in_addr);
    assert (result == 1&&in_addr.s_addr == ((23<<24) | (7<<16) | (0<<8) | 130));
    result = inet_pton ("130.0.7.23.",&in_addr);
    assert (result == 0);
    return (0);
}
c
  • Answer # 1

    The last character should be taken to == '.'.
    The string length l can be determined by using strlen, and src [l] is the terminal character, so the previous character is used for comparison.

    If you want to make sure it ends with astring, write! strcmp (src + n, "hoge").
    You need to calculate in advance where a particular string can start.

  • Answer # 2

    This function gets the last character
    How about this?

    char right (char * str) {
       int len ​​= strlen (str);
       return 0