本文目录一览:
C语言不用string.h将数值转化为字符串
#include stdio.h
double toDouble(char dstr[]) {
double decimal = 0,integer = 0,times = 10;
int i = 0,sign = 1;
if(dstr[i] == '-') {
sign = -1;
++i;
}
while(dstr[i] dstr[i] != '.') {
if(dstr[i] = '0' dstr[i] = '9')
integer = 10 * integer + dstr[i] - '0';
++i;
}
if(dstr[i] == '.') {
while(dstr[i]) {
if(dstr[i] = '0' dstr[i] = '9') {
decimal += (dstr[i] - '0')/times;
times *= 10;
}
++i;
}
}
return sign * (integer + decimal);
}
int toInteger(char istr[]) {
int i = 0,num = 0,sign = 1;
if(istr[i] == '-') { sign = -1; ++i; }
while(istr[i]) {
if(istr[i] = '0' istr[i] = '9')
num = 10 * num + istr[i] - '0';
++i;
}
return sign * num;
}
int main() {
char s[] = "36.56";
char t[] = "127";
printf("%lf,%d\n",toDouble(s),toInteger(t));
return 0;
}
C语言不用string.h把两个不同的字符串合并成一个
char *my_strcat(char * const d,char *s)
{
char *p=d;
while(*p)p++;
while(*p++=*s++);
return d;
}
C语言不用string.h实现字符串中出现的大写字母转化为小写字母
下面的程序先循环读取字符,判断是否为大写字母,即是否在A至Z之间。如果是大写字母,就加32,这样字符的ASCII码就改变了。比如大写A,ASCII码为65,加32变为97,就是小写a了。
#includestdio.h
#includestdlib.h
int main()
{
char str[20]="ABCdefGhijKL";
int i;
printf("before change,string is %s\n",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]='Z' str[i]='A')
{
str[i]=str[i]+32;
}
}
printf("after change,string is %s\n",str);
return 0;
}
运行结果如下,可以看到字符串中的大写字母变为了小写字母。