//1. 使用库函数将数字转换为字符串
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(int argc , char *argv[])
{
int num;
char str[30];
printf("input temp numble:");
scanf("%d",&num);
sprintf(str,"%d",num);
printf("字符串:%s\n",str);
return 0;
}
//2. 不使用库函数将数字转换为字符串
void int_to_char(int n,char *str)
{
int i = 0;
int len = 0;
char buf[10] = "";
int temp = n < 0 ? -n : n;
if(str == NULL)
{
return;
}
while(temp)
{
buf[i++] = (temp % 10) + '0';
temp = temp/10;
}
len=n<0?++i:i;
str[i]=0;//str[2]=0; ASCII '\0'=0
while(1)
{
i--;
if(buf[len-i-1]==0)
{
break;
}
str[i] = buf[len-i-1];
}
if(i == 0)
{
str[i] = '-';
}
}
int main(int argc , char *argv[])
{
int n;
char p[10];
n=-13456;
int_to_char(n,p);
printf("The char is:%s\n",p);
return 0;
}
//3. 使用库函数将字符串转换为数字
int main(int argc , char *argv[])
{
char str[30];
int i;
printf("input the char:");
scanf("%s",str);
i = atoi(str);
printf("output the numble:%d\n",i);
}
//4. 不使用库函数将字符串转换为数字
int str_to_num(char * str)
{
int temp=0;
char * p = str;
if(* str=='+' || * str=='-')
{
str++;
}
while(*str!='\0')
{
if(*str>='0'&&*str<='9')
{
temp=temp*10+(* str-'0');
str++;
}
else
{
printf("intput error!");
break;
}
}
if(* p == '-')
{
temp=-temp;
}
return temp;
}
int main(int argc , char *argv[])
{
int n;
char p[10];
printf("intput the char:");
scanf("%s",p);
n=str_to_num(p);
printf("output the number:%d\n",n);
}
//5. 编程实现strcpy函数
int main(int argc , char *argv[])
{
int i=0;
char s1[10];
char s2[10];
printf("input you array:");
gets(s1);
while(s1[i]!='\0')
{
s2[i]=s1[i];
i++;
}
s2[i]='\0';
printf("s1=%s\ns2=%s\n",s1,s2);
return 0;
}
//6. 编程实现strcat函数
int main(int argc, char* argv[])
{
char s[40],s1[20],s2[20];
int i,j;
printf("input the first array:");
scanf("%s",s1);
printf("input the second array:");
scanf("%s",s2);
for(i=0;i<strlen(s1);)
{
s[i]=s1[i];
i=i+1;
}
// printf("%d",i);
for(j=0;j<strlen(s2);j++)
{
s[j+i]=s2[j];
}
s[i+j]='\0';
printf("%s\n",s);
}
//7. 编程实现字符串长度检测
int main(int argc, char* argv[])
{
char str[20];
int n=0;
printf("input the array:");
scanf("%s",str);
while(str[n]!='\0')
n++;
printf("The length of %s is %d\n",str,n);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(int argc , char *argv[])
{
int num;
char str[30];
printf("input temp numble:");
scanf("%d",&num);
sprintf(str,"%d",num);
printf("字符串:%s\n",str);
return 0;
}
//2. 不使用库函数将数字转换为字符串
void int_to_char(int n,char *str)
{
int i = 0;
int len = 0;
char buf[10] = "";
int temp = n < 0 ? -n : n;
if(str == NULL)
{
return;
}
while(temp)
{
buf[i++] = (temp % 10) + '0';
temp = temp/10;
}
len=n<0?++i:i;
str[i]=0;//str[2]=0; ASCII '\0'=0
while(1)
{
i--;
if(buf[len-i-1]==0)
{
break;
}
str[i] = buf[len-i-1];
}
if(i == 0)
{
str[i] = '-';
}
}
int main(int argc , char *argv[])
{
int n;
char p[10];
n=-13456;
int_to_char(n,p);
printf("The char is:%s\n",p);
return 0;
}
//3. 使用库函数将字符串转换为数字
int main(int argc , char *argv[])
{
char str[30];
int i;
printf("input the char:");
scanf("%s",str);
i = atoi(str);
printf("output the numble:%d\n",i);
}
//4. 不使用库函数将字符串转换为数字
int str_to_num(char * str)
{
int temp=0;
char * p = str;
if(* str=='+' || * str=='-')
{
str++;
}
while(*str!='\0')
{
if(*str>='0'&&*str<='9')
{
temp=temp*10+(* str-'0');
str++;
}
else
{
printf("intput error!");
break;
}
}
if(* p == '-')
{
temp=-temp;
}
return temp;
}
int main(int argc , char *argv[])
{
int n;
char p[10];
printf("intput the char:");
scanf("%s",p);
n=str_to_num(p);
printf("output the number:%d\n",n);
}
//5. 编程实现strcpy函数
int main(int argc , char *argv[])
{
int i=0;
char s1[10];
char s2[10];
printf("input you array:");
gets(s1);
while(s1[i]!='\0')
{
s2[i]=s1[i];
i++;
}
s2[i]='\0';
printf("s1=%s\ns2=%s\n",s1,s2);
return 0;
}
//6. 编程实现strcat函数
int main(int argc, char* argv[])
{
char s[40],s1[20],s2[20];
int i,j;
printf("input the first array:");
scanf("%s",s1);
printf("input the second array:");
scanf("%s",s2);
for(i=0;i<strlen(s1);)
{
s[i]=s1[i];
i=i+1;
}
// printf("%d",i);
for(j=0;j<strlen(s2);j++)
{
s[j+i]=s2[j];
}
s[i+j]='\0';
printf("%s\n",s);
}
//7. 编程实现字符串长度检测
int main(int argc, char* argv[])
{
char str[20];
int n=0;
printf("input the array:");
scanf("%s",str);
while(str[n]!='\0')
n++;
printf("The length of %s is %d\n",str,n);
return 0;
}