0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【C言語】整数のいずれかの桁に特定の数字が含まれているかを確認する

Posted at

簡単に実装できそうな割には、調べてもあまり出てこなかったためメモ書きとして残しておく~~(調べ方が悪いだけ)~~。
適当に書いたため結構ガバガバなコードかも。なにか指摘あればお願いします。
なおバッファオーバーフロー時の処理を省いているので、ちゃんと実装したい時にはその辺もしっかり考える必要あり。

#include <stdio.h>
int check(int n, char c){
    int i = 0, f = 0;
    char suzi[64];
    snprintf(suzi, sizeof(suzi), "%d", n);
    while(suzi[i] != '\0'){
        if(suzi[i] == c){
            f = n;
            break;
        }
        i++;
    }
    return f;
    //確認したい数字が含まれている場合は整数をそのまま返す
    //確認したい数字が含まれていない場合は0を返す
}

int main(void){
int n = 123450;   //調べたい整数
char c = '0';    //確認したい数字
printf("%d\n", check(n, c));
// 実行結果> 123450
return 0;
}
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?