strstr()与strchr()、竖式问题

strstr()与strchr()都包含着<string.h>中
strchr() 字符串与字符串
    原型:char * strchr(char * str, int ch)
    功能:找出在字符串str中第一次出现字符ch的位置,找到就返回该字符位置的指针(也就是返回该字符在字符串中的地址的位置),找不到就返回空指针(就是 null)。
strstr() 字符串与字符
    原型: char * strstr(char * str1,char * str2)
    功能:找出在字符串str1中第一次出项字符串str2的位置(也就是说字符串sr1中要包含有字符串str2),找到就返回该字符串位置的指针(也就是返回字符串str2在字符串str1中的地址的位置),找不到就返回空指针(就是 null)。
P41 竖式问题
#include <stdio.h>
#include <string.h>
int main()
{
    char s[20],buf[99];
    int abc,de,x,y,z;
	int i,ok = 1,count = 0;
    scanf("%s",s);
    for(abc = 111;abc<=999;abc++)
    {
        for(de = 11;de<=99;de++)
        {
            x = abc*(de%10);
            y = abc*(de/10);
            z = abc*de;
            sprintf(buf,"%d%d%d%d%d",abc,de,x,y,z); //sprintf() 把信息输出到字符串,应保证字符串足够大可以容纳输出信息
            ok = 1;
            for(i = 0;i<strlen(buf);i++)
            {
            	if(strchr(s,buf[i])==NULL) ok = 0;
            }
            if(ok)
            {
            	printf("<%d>\n",++count);
        		printf("%5d\nX%4d\n-----\n%5d\n%4d\n-----%5d\n\n",abc,de,x,y,z);
            }
        }   
    }
     printf("The number of solutions = %d",count);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c_y_w_/article/details/79910426
今日推荐