C言語の基礎 |
![]() ![]() ![]() |
4-3 文字処理関数
(1) isalpha
#include <ctype.h>
int isalpha(int c);
文字cが英字(0x41〜0x5aまたは0x61〜0x7a)であるかを判定する。
返却値は、判定結果が正しければ0以外。正しくなければ0。
(2) isdigit
#include <ctype.h>
int isdigit(int c);
文字cが数字(0x30〜0x39)であるかを判定する。
返却値は、判定結果が正しければ0以外。正しくなければ0。
(3) isupper
#include <ctype.h>
int isupper(int c);
文字cが英大文字(0x41〜0x5a)であるかを判定する。
返却値は、判定結果が正しければ0以外。正しくなければ0。
(4) islower
#include <ctype.h>
int islower(int c);
文字cが英小文字(0x61〜0x7a)であるかを判定する。
返却値は、判定結果が正しければ0以外。正しくなければ0。
・入力した1文字が英字かどうかを判定する。
/* sample29.c */
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
ch = getchar();
if(isalpha(ch))
puts("alpha");
else
puts("not alpha");
}
(5) toupper
#include <ctype.h>
int toupper(int c);
文字cが英小文字であれば英大文字に変換する。
返却値は、変換した文字。英小文字以外は、そのまま。
(6) tolower
#include <ctype.h>
int tolower(int c);
文字cが大文字であれば英小文字に変換する。
返却値は、変換した文字。英大文字以外は、そのまま。
・入力した1文字が英小文字であれば大文字に変換して表示する。
/* sample30.c */
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
ch = getchar();
ch = toupper(ch);
putchar(ch);
}
4-4 文字列処理関数
(1) strcpy
#include <string.h>
char *strcpy(char *str1, char *str2);
char *strncpy(char *str1, char *str2, int size);
str2が示す文字列をstr1が示すバッファにコピーする。strncpyはsize文字分だけをコピーする。
返却値は、バッファstr1の先頭アドレス。
・文字列を配列にコピーしてから表示する。
/* sample31.c */
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[128];
strcpy(str1, "abcde");
printf("%s\n", str1);
}
abcde |
(2) strcat
#include <string.h>
char *strcat(char *str1, char *str2);
char *strncat(char *str1, char *str2, int size);
str2が示す文字列をstr1が示すバッファに格納されている文字列の最後にコピーして連結する。strncatはstr2が示す文字列を最大size文字分だけ連結する。
返却値は、バッファstr1の先頭アドレス。
・2つの文字列を連結して、表示する。
/* sample32.c */
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[128];
char *str2 = "12345";
strcpy(str1, "abcde");
strcat(str1, str2);
printf("%s\n", str1);
}
abcde12345 |
(3) strcmp
#include <string.h>
int strcmp(char *str1, char *str2);
int strncmp(char *str1, char *str2, int size);
str2が示す文字列とstr1が示す文字列を比較する。strncmpはsize文字分だけを比較する。
返却値は、str1 > str2のときは正、str1 == str2のときは0、str1 < str2のときは負。
・2つの文字列が同じかどうかを判定する。
/* sample33.c */
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[128], str2[128];
gets(str1);
gets(str2);
if(!strcmp(str1, str2))
puts("onaji");
else
puts("chigau");
}
(4) strlen
#include <string.h>
int strlen(char *str);
strが示す文字列の文字数を計算する。
返却値は、文字数。
・入力された文字列の長さ(文字数)を表示する。
/* sample34.c */
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[128];
int len;
gets(str);
len = strlen(str);
printf("%d\n", len);
}
(5) strchr
#include <string.h>
char *strchr(char *str,int c);
strが示す文字列の中から文字cが最初に現れる位置を探す。
返却値は、文字cの現れた位置のアドレス。見つからなかったときは、NULL(ナル文字 \0)。
・入力した文字列の中に入力した1文字があるかどうか検索し、あればその文字以降を表示する。
/* sample35.c */
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[128], *pa;
int ch;
gets(str);
ch = getchar();
pa = strchr(str, ch);
if(pa == NULL)
puts("not found");
else
printf("%s\n", pa);
}
(6) atoi
#include <stdlib.h>
int atoi(char *str);
strが示す文字列を整数値に変換する。
返却値は、変換された整数値。
・入力された数字の文字列を整数値に変換して表示する。
/* sample36.c */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char str[128];
int x;
gets(str);
x = atoi(str);
printf("%d\n", x);
}
![]() ![]() ![]() |
Copyright © 2001-2003 Hiroshi Masuda |