プログラミング+α学習ブログ

勉強したことの備忘録です。

配列を受け渡すプログラム

#include <stdio.h> /*--- 配列の先頭三要素に1,2,3を代入 ---*/ void set123(int *vc) { *vc = 1; // vc[0] = 1; *(vc + 1) = 2; // vc[1] = 2; vc[2] = 3; // vc[2] = 3; } int main(void) { int i; int ary[5] = { 0 }; // 全ての要素を0で初期化 set123(ary); for</stdio.h>…

ポインタの比較と減算をするプログラム

#include <stdio.h> int main(void) { int vc[5]; int *ptr = vc; printf("ptr == vc : %d\n", ptr == vc); printf("ptr == &vc[0] : %d\n", ptr == &vc[0]); printf("ptr == &vc[1] : %d\n", ptr == &vc[1]); printf("&vc[1] < &vc[2] : %d\n", &vc[1] < &vc[2]); pr</stdio.h>…

配列とポインタを用いて各要素を表示するプログラム

実行結果

三つの整数を昇順に並べ替える

#include <stdio.h> /*--- nx, nyが指すオブジェクトの値を交換 ---*/ void swap(int *nx, int *ny) { int temp = *nx; *nx = *ny; *ny = temp; } /*--- *n1≦*n2≦*n3となるように並べ替える ---*/ void sort3(int *n1, int *n2, int *n3) { if (*n1 > *n2) swap(n1, n</stdio.h>…

二つの整数を昇順に並べるプログラム

実行結果

読み込んだ日付の前の日付および次の日付を求めるプログラム

#include <stdio.h> int day[2][13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, }; /*--- yearが閏年であれば1をそうでなければ0を返す ---*/ int isleap(int year) { return (year % 4 ==</stdio.h>…

二つの整数の和、差、積を求めるプログラム

#include <stdio.h> /*--- n1とn2の和・差・積をsum,diff,mulが指す変数に格納 ---*/ void sum_diff_mul(int n1, int n2, int *sum, int *diff, int *mul) { *sum = n1 + n2; *diff = (n1 > n2) ? n1 - n2 : n2 - n1; *mul = n1 * n2; } int main(void) { int na, nb;</stdio.h>…

二つの整数の値を交換するプログラム

#include <stdio.h> int main(void) { int n1, n2, tmp; int *np1 = &n1, *np2 = &n2; puts("二つの整数を入力してください。"); printf("整数A:"); scanf("%d", &n1); printf("整数B:"); scanf("%d", &n2); tmp = *np1; *np1 = *np2; *np2 = tmp; puts("これらの値</stdio.h>…

静的記憶域期間が与えられたオブジェクトのアドレスを表示するプログラム

#include <stdio.h> int nx; int main(void) { static double dx; static int vc[3]; printf("nxのアドレス:%p\n", &nx); printf("dxのアドレス:%p\n", &dx); printf("vc[0]のアドレス:%p\n", &vc[0]); printf("vc[1]のアドレス:%p\n", &vc[1]); printf("vc[2]の</stdio.h>…

文字列中の数学文字を削除するプログラム

#include <stdio.h> /*--- 文字列str中の数字文字を削除する ---*/ void del_digit(char str[]) { int i = 0, idx = 0; while (str[i]) { if (str[i] < '0' || str[i] > '9') str[idx++] = str[i]; i++; } str[idx] = '\0'; } int main(void) { char str[100]; printf</stdio.h>…

文字列を空文字列にするプログラム

#include <stdio.h> /*--- 文字列strを空文字列にするプログラム ---*/ void null_string(char str[]) { str[0] = '\0'; } int main(void) { char str[] = "ABC"; printf("str=%s\n", str); null_string(str); // 文字列strを空文字列にする puts("文字列strを空文字</stdio.h>…

文字列の配列を表示するプログラム

実行結果

文字列を逆順に格納するプログラム

#include <stdio.h> /*--- 文字列strの長さを返す ---*/ unsigned str_length(const char str[]) { unsigned len = 0; while (str[len]) len++; return (len); } /*--- 文字列を逆順に並べ替える ---*/ void rev_string(char str[]) { int i; int len = str_length(st</stdio.h>…

文字列を後ろから逆に表示するプログラム

#include <stdio.h> /*--- 文字列strの長さを返す ---*/ unsigned str_length(const char str[]) { unsigned len = 0; while (str[len]) len++; return (len); } /*--- 文字列を逆から表示 ---*/ void put_rstring(const char str[]) { unsigned i = str_length(str);</stdio.h>…

文字列を指定された回数だけ連続して表示するプログラム

#include <stdio.h> /*--- 文字列strをno回表示する ---*/ void put_stringn(const char str[], int no) { while (no-- > 0) printf("%s", str); } int main(void) { char str[100]; int no; printf("文字列を入力してください:"); scanf("%s", str); printf("何回表</stdio.h>…

文字列をそのまま表示するプログラム(2)

#include <stdio.h> /*--- 文字列を表示 ---*/ void put_string(const char str[]) { printf("%s", str); } int main(void) { char str[100]; printf("文字列を入力してください:"); scanf("%s", str); put_string(str); putchar('\n'); return (0); } 実行結果 文字</stdio.h>…

文字列を一文字ずつなぞりながら表示するプログラム

#include <stdio.h> /*--- 文字列を表示 ---*/ void put_string(const char str[]) { unsigned i = 0; while (str[i]) putchar(str[i++]); } int main(void) { char str[100]; printf("文字列を入力してください:"); scanf("%s", str); put_string(str); putchar('\n</stdio.h>…

文字列に含まれる特定文字の個数を返すプログラム

#include <stdio.h> /*--- 文字列str中の文字cの個数を返す ---*/ int str_chnum(const char str[], int c) { int i; int count = 0; for (i = 0; str[i] != '\0'; i++) if (str[i] == c) count++; return (count); } int main(void) { char str[256]; char ch[10]; p</stdio.h>…

文字列に特定文字が含まれているかどうかを調べるプログラム

#include <stdio.h> /*--- 文字列strから文字cを検索し、最も先頭側の要素の添字を返す ---*/ int str_char(const char str[], int c) { int i; for (i = 0; str[i] != '\0'; i++) if (str[i] == c) return (i); return (-1); } int main(void) { int no; char ch[10]</stdio.h>…

文字列の配列を用意し、標準入力から読み込んだ文字列を表示するプログラム

#include <stdio.h> int main(void) { int i; char cs[3][128]; // 各文字列は最大128文字を格納 for (i = 0; i < 3; i++) { printf("cs[%d]:", i); scanf("%s", cs[i]); } for (i = 0; i < 3; i++) printf("cs[%d]=\"%s\"\n", i, cs[i]); return (0); } 実行結果 cs</stdio.h>…

2次元配列に格納した文字列を表示するプログラム

#include <stdio.h> int main(void) { int i; char cs[][5] = {"LISP", "C++", "Ada"}; for (i = 0; i < 3; i++) printf("cs[%d]=\"%s\"\n", i, cs[i]); return (0); } 実行結果 cs[0]="LISP" cs[1]="C++" cs[2]="Ada"</stdio.h>

文字列を流しながら表示するプログラム

実行結果

名前を尋ねて挨拶するプログラム

#include <stdio.h> int main(void) { char name[40]; printf("お名前は:"); scanf("%s", name); printf("はじめまして、%sさん!", name); return (0); } 実行結果 お名前は:Shibata はじめまして、Shibataさん!</stdio.h>

文字列を空文字列にするプログラム

#include <stdio.h> int main(void) { char str[] = "ABC"; printf("str=%s\n", str); // strは"ABC" str[0] = '\0'; // 配列の先頭要素にナル文字を代入 puts("文字列strを空文字列にしました。"); printf("str=%s\n", str); // strは空文字列 return (0); } 実行結</stdio.h>…

ABC\0DEFで初期化された文字列を表示するプログラム

#include <stdio.h> int main(void) { char str[] = "ABC\0DEF"; // 途中にナル文字がある printf("文字列strは%sです。\n", str); return (0); } 実行結果 文字列strはABCです。</stdio.h>

配列に文字列を格納して表示するプログラム(2)

#include <stdio.h> int main(void) { char str[] = "String"; // 初期化 printf("文字列strは%sです。\n", str); return (0); } 実行結果 文字列strはStringです。</stdio.h>

配列に文字列を格納して表示するプログラム(1)

#include <stdio.h> int main(void) { char str[7]; // 文字列を格納する配列 str[0] = 'S'; str[1] = 'T'; str[2] = 'R'; str[3] = 'I'; str[4] = 'N'; str[5] = 'G'; str[6] = '\0'; printf("文字列strは%sです。\n", str); return (0); } 実行結果 文字列strはSTRI</stdio.h>…

入力に現れた改行の数を表示するプログラム

#include <stdio.h> int main(void) { int ch; int n_count = 0; // 改行文字の数 while ((ch = getchar()) != EOF) if (ch == '\n') n_count++; printf("行数:%d\n", n_count); return (0); } 実行結果 Hello! This is a pen. 行数:2</stdio.h>

標準入力からの入力を標準出力にコピーするプログラム

#include <stdio.h> int main(void) { int ch; while ((ch = getchar()) != EOF) putchar(ch); return (0); } 実行結果 Hello! Hello! This is a pen. This is a pen.</stdio.h>

入力に出現する数字をカウントして縦向きの棒グラフで表示するプログラム

#include int main(void) { int i, j, ch; int cnt_max = 0; // 出現回数の最大値 int cnt[10] = {0}; // 数字文字の出現回数 while (1) { // 無限ループ ch = getchar(); if (ch == EOF) break; if (ch >= '0' && ch cnt_max) cnt_max = cnt[i]; puts("数字…