どうも、ちょげ(@chogetarou)です。
文字列(string)の末尾の文字を取得する方法を紹介します。
方法

文字列(string)の最後の文字を取得するには、インデックスを使います。
まず、文字列の文字数を取得します。
そして、文字列の文字数を「-1」した値のインデックスにアクセスします。
//text=対象の文字列
int count = strlen(text); //文字数を取得
char last = text[count - 1]; //最後の文字を取得
文字数を「-1」したインデックスにアクセスすることで、対象の文字列の最後の文字を取得できます。
使用例
#include <stdio.h>
int main(void){
char text[] = "Hello, World";
int count = strlen(text);
char last = text[count - 1];
printf("%c", last);
return 0;
}
出力:
d
コメント