1.9实现函数atoi,将输入的字符串转化为数字。(输入可能包含非数字字符)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, const char *argv[])
{
char str[20];
gets(str);
int i = 0;
int num = 0;
for(i=0;i<strlen(str);i++){
if(str[i]>='0'&&str[i]<='9')
num = 10*num + str[i]-'0';
else
continue;
}
printf("num = %d\n",num);
return 0;
}
⒉.初始化两个字符串,使用非函数实现字符串的比较
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, const char *argv[])
{
char str1[100]="abcdefg";
char str2[100]="ABCDEFG";
int i = 0;
while(str1[i]==str2[i]){
if(str1[i]=='\0' || str2[i]=='\0')
break;
i++;
}
if(str1[i]>str2[i])
printf("str1>str2\n");
else if(str1[i]<str2[i])
printf("str1<str2\n");
else if(str1[i]==str2[i])
printf("str1=str2\n");
return 0;
}
3.不使用系统函数实现strcat( )
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, const char *argv[])
{
char str1[100]="hello ";
char str2[]="world";
int i = 0;
while(str1[i]!='\0')
i++;
int j = 0;
while(str2[j]!='\0'){
str1[i]=str2[j];
i++;
j++;
}
str1[i]='\0';
puts(str1);
puts(str2);
return 0;
}
4.输入两个字符串实现交换strcpy
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, const char *argv[])
{
char str1[100]="abcdefgh";
char str2[]="1234";
int i = 0;
while(str2[i]!='\0'){
str1[i]=str2[i];
i++;
}
str1[i]='\0';
puts(str1);
return 0;
}
5.思维导图,把课堂练习题在做一遍