day7作业

1.杨辉三角形

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	int m;
	printf("请输入行数:");
	scanf("%d",&m);
	
	int arr[m][m];
	
	for(int i=0; i<m; i++)
	{	
		for(int k=m-1;k>i;k--)
			{
				printf("   ");
			}
	
		for(int j=0; j<=i; j++)
		{
			if(0==j || j==i)
			{
				arr[i][j]=1;
			}
			else
			{
				arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
			}
			printf("%6d",arr[i][j]);
		}
		printf("\n");
	} 		
	return 0;
}

 2.图形打印

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	int arr[4][4];
	char a='A';
	for(int i=0; i<4; i++)
	{
		for(int j=0; j<i+1; j++)
		{
			printf("%c",a);
		}
		a=a+1;
		printf("\n");
	}

	printf("\n");

	int k[4][4];
	char b='D';
	for(int i=0;i<4;i++)
	{
		for(int j=0;j<4-i;j++)
		{
			printf("%c",b);
		}
		b=b-1;
		printf("\n");
	}
	return 0;
}

 3.谁是凶手

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	char x;
	for(x='A'; x<='D';x++)
	{
		if(('A'!=x) + ('c'==x)*2 + (x=='A' || x=='D') == 2)
		{
   			printf("凶手是%c\n",x);
		}
	}

	return 0;
}

 4.判断

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	int num,count=0;
	printf("请输入一个整数");
	scanf("%d",&num);

	if(num%3==0)
	{
		printf("3 ");
		count++;
	}
	if(num%5==0)
	{
		printf("5 ");
		count++;
	}
	if(num%7==0)
	{
		printf("7 ");
		count++;
	}
	if(count==0)
	{
		printf("n");
	}
	printf("\n");
	return 0;
}

 5.字符串单词的逆置

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	char str[30];

	printf("请输入字符串:");
	gets(str);

	int i=0, j=strlen(str)-1;

	while(i<j)
	{
		char temp=str[i];
		str[i]=str[j];
		str[j]=temp;
		i++;
		j--;
	}

	printf("%s\n",str);

	i=0,j=0;

	while(str[i]!='\0')
	{
		while(str[j]!=' ' && str[j]!='\0')
		{
			j++;
		}
		int k=j-1;
		while(i<k)
		{
			char temp=str[i];
			str[i]=str[k];
			str[k]=temp;
			i++;
			k--;
		}
		while(str[j]==' ')
		{
			j++;
		}
		i=j;
	}

	printf("%s\n",str);


	return 0;
}

 6.atoi的实现

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	char str[]="1234";
	int i=0,sum=0;

	while(str[i])
	{
		int n=str[i]-'0';
		sum=sum*10+n;
		i++;
	}

	printf("sum=%d\n",sum);

	return 0;
}

7.输入一个字符串,删除字符串中的空格

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{

	char str1[30];
	char str2[30];
	printf("请输入字符串:");
	gets(str1);

	int i=0, n=strlen(str1),j=0;

	for(i ;i<n ;i++)
	{
		if(str1[i]!=' ')
		{
			str2[j]=str1[i];
			j++;
		}
	}
	str2[j]=0;
	printf("%s\n",str2);

	return 0;
}

 

 8.1字符串长度strlen

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	char str[]="abcdefg";
	int count=0;
	while(str[count])
	{
		count++;
	}

	printf("字符串长:%d\n",count);

	return 0;
}

 8.2字符串拷贝strcpy

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	char dest[20]="hello";
	char str[]="ADS";

	printf("dest[20]=%s\n",dest);
	printf("srt[]=%s\n",str);

	int i=0;
	while(str[i])
	{
		dest[i]=str[i];
		i++;
	}
	dest[i]=0;
	
	printf("dest[20]=%s\n",dest);

	return 0;
}

8.3字符串连接strcat

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	char dest[20]="I need more ";
	char str[]="pwoer";

	printf("dest[20]=%s\n",dest);
	printf("str[]=%s\n",str);
//	strcat(dest,str);

	int i=0,j=0;

	while(dest[i])
	{
		i++;
	}

	while(str[j])
	{
		dest[i]=str[j];
		i++;
		j++;
	}
	dest[i]=0;

	printf("dest[20]=%s\n",dest);

	return 0;
}

 

 8.4字符串比较strcmp

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char *argv[])
{
	char s1[]="abcd";
	char s2[]="abCD";

	printf("s1[]=%s\n",s1);
	printf("s2[]=%s\n",s2);

	int i=0;
	while(s1[i]==s2[i])
	{
		if(s1[i]==0) break;
		i++;
	}

	if(s1[i]>s2[i])
		printf("s1>s2\n");
	else if(s1[i]<s2[i])
		printf("s1<s2\n");
	else
		printf("s1=s2\n");

	return 0;
}

 

思维导图:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值