秋田大学ICPC対策室@wiki

文字列処理関数(string.h)

最終更新:

akitaicpc

- view
だれでも歓迎! 編集

文字列処理関数


<string.h>が必要となります。



メモリの中で特定の文字列を探す
memchr(const *pbuf, int c, size_t MaxCount)
引数 pbuf:文字を探すメモリバッファのポインタ
     c:探す文字
     MaxCount:文字数
戻り値:文字があればその文字のポインタ,なければNULL
+ プログラム例
#include <stdio.h>
#include <string.h>

int main(){
	char pbuf[80]="Hello World!";
	char c;
	int flag;

	printf("文字列 -> %s \n",pbuf);

	printf("探したい1文字入力:");
	scanf("%c", &c);

	if( memchr( pbuf , c , strlen(pbuf) ) != NULL)
		printf("%sの中に%cは存在します \n" , pbuf , c);
	else
		printf("%sの中に%cは存在しません \n" , pbuf , c);

	return 0;
}


2つの文字列を連結する
strcat(char *dest, const char *source)
引数 dest:先頭の文字列
     source:後ろに連結する文字列
戻り値:連結された後の文字列destのポインタ
+ プログラム例
#include <stdio.h>
#include <string.h>

int main(){
	char str1[80] = "Hello";
	char str2[] = "World!";

	strcat(str1,str2);
	printf("%s \n", str1 );
}

実行結果
HelloWorld!


文字列の長さを求める
strlen(const char *str)
引数 str:長さを求めたい文字列
戻り値:文字数
+ プログラム例
#include <stdio.h>
#include <string.h>

int main(){
	char str[80];

	printf("文字列を入力してください:");
	scanf("%s", str);
	printf("入力した文字列の文字数は%dです \n", strlen(str) );

	return 0;
}



...

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

目安箱バナー