11.7整理

有道云笔记

1含有非数字字符的数字转换

#include <stdio.h>
int main(int argc, const char *argv[])
{
	char a[32]="";
	gets(a);
	int i=0,sum=0;
	while(a[i]!='\0')
	{
		
		if(a[i]>='9'||a[i]<='0')
		{
			i++;
			continue;
		}
			
		else if(a[i]<='9'&&a[i]>='0')
		{
			a[i]=a[i]-48;
		     sum=sum*10+a[i];
			 i++;
		}	
	}
	printf("%d\n",sum);

	return 0;
}

2输入字符串并比较。

#include <stdio.h>
int main(int argc, const char *argv[])
{
	
	char a[32]="";
	char b[32]="";
	gets(a);
	gets(b);
	int i=0;
	int reg=0;
	while(a[i]!='\0'||b[i]!='\0')
	{
		if(a[i]-b[i]>0)
		{
			reg=1;
			break;
		}
		else if(a[i]-b[i]<0)
		{
			reg=-1;
			break;
		}

			i++;	
	}
	if(reg==0)
		printf("a=b\n");
	if(reg==1)
		printf("a>b\n");
	if(reg==-1)
		printf("a<b\n");

	return 0;
}

3非函数实现strcat

#include <stdio.h>
int main(int argc, const char *argv[])
{
	char a[32]="";
	char b[32]="";
	gets(a);
	gets(b);
	int i=0,j=0;
	while(a[i]!='\0')
		i++;
	while(b[j]!='\0')
	{
	
		a[i]=b[j];	
		j++;
		i++;
	
	}
	a[i]='\0';
	printf("%s\n",a);
	return 0;
}

4非函数实现strcpy

#include <stdio.h>
int main(int argc, const char *argv[])
{
	char a[32]="";
	char b[32]="";
	gets(a);
	gets(b);
	int i=0;
	int j=0;
	while(b[i]!='\0')
		i++;
	for(j=0;j<=i;j++)
	{
		a[j]=b[j];

	}
	a[j]='\0';
printf("%s\n",a);
	return 0;
}

课堂练习

单词倒置

#include <stdio.h>
#include "string.h"
int main(int argc, const char *argv[])
{
	char s[32]="good good  study";
	int i=0,j=0;
	char t=0;int tp=0;
	printf("%s\n",s);
	while(s[j]!='\0')
		j++;
	j--;
	while(i<j)
	{
		t=s[i];
		s[i]=s[j];
		s[j]=t;
		i++;
		j--;
	}
	printf("%s\n",s);
	i=0;j=0;
	while(s[tp]!='\0')
	{

		i=tp;
		while(s[i]==' ')
			i++;
		j=i;
		while(s[j]!=' '&&s[j]!='\0')
			j++;
		tp=j;
		j--;
		while(i<j)
		{

			t=s[i];
			s[i]=s[j];
			s[j]=t;
			i++;
			j--;

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

	return 0;
}

杨辉三角、

#include <stdio.h>
int main(int argc, const char *argv[])
{
	int i=0,j=0;
	int a[100][100]={0};
	int n=0;
	printf("输入行数n\n");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		a[i][0]=1;
		for(j=1;j<=i;j++)
		{
			a[i][j]=a[i-1][j]+a[i-1][j-1];
	
		}
	
	}

	for(i=n-2;i>=0;i--)
	{
		a[i][n-1]=1;
#if 1	
		for(j=n-2;j>i;j--)
		{
			a[i][j]=a[i+1][j]+a[i+1][j+1];		
		}
#endif	
 	
	}


	for(i=0;i<n;i++)
	{
		for(j=0;j<=i;j++)
			printf("%4d",a[i][j]);
		for(j;j<n;j++)
			printf("%4d",a[i][j]);
		printf("\n");
	}



	return 0;
}

aoti

#include <stdio.h>
int main(int argc, const char *argv[])
{
	
	char s[32]="";
	gets(s);
	int i=0;
	int sum=0;
	printf("%s\n",s);
		while(s[i]!='\0')
	{
		s[i]=s[i]-48;
		sum=sum*10+s[i];
	i++;
	}
	printf("%d\n",sum);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值