サンプル C プログラム

文字列結合

※ 実用には、標準関数のstrcatを使用する。 -- #include <stdio.h> void Bind_Str(char *s1, char *s2); int main(void) { char str1[100] = "Hello"; char str2[]="World"; Bind_Str(str1, str2); printf("%s\r\n", str1); return 0; } // 文字列s1に文字列s2を付加</stdio.h>…

多文字列の格納。

// 20個の文字列を格納する。1文字列あたり100文字。 // 格納後に表示。 #include <stdio.h> int main(void) { char str[20][100]; for(int i=0; i<20; i++) { scanf("%s", str[i]); } printf("\r\nkekkka\r\n"); for(int k=0; k<20; k++) { printf("%s\r\n", str[k]</stdio.h>…

小文字列を大文字列化

#include <stdio.h> #include <ctype.h> //toupper用 void ToBig(char *s); //プロトタイプ宣言 int main(void) { char str[100]; scanf("%s", str); ToBig(str); printf("%s", str); return 0; } void ToBig(char *s) { while(*s) { *s = toupper(*s); s++; } }…</ctype.h></stdio.h>

16進数 → 2進数 変換

16進数 → 2進数 変換 再作成することがあるので記載。 #include <stdio.h> #include <string.h> int main(void) { char hex; char binary[5]; scanf("%c", &hex); switch(hex) { case '0': strcpy(binary, "0000"); break; case '1': strcpy(binary, "0001"); break; case '2':</string.h></stdio.h>…

ファイル一覧を取得する。

stdlib.h の system()関数を用いて "ls"コマンドを実行できます。が、その結果を取得することができません。 -- いろいろなサイトを見て、 stdlib.h の popen()関数を用いて "ls"コマンドの結果を取得することができました。 ~~~~~~~~ ソース ~~~~~~~~~ #inc…

max_of_array --配列中の最大値を求める。

int max_of_array(int n, int len); 戻値: 引数で入力した配列要素の最大値 引数1: 配列 引数2: 配列長(= 配列の要素数) --------------------------------------------- #include <stdio.h> int max_of_array(int n, int len); int main(void) { int x[10]; for</stdio.h>…