字符串复制与字符串链接两个这两个方面在实现那一块有点不是很懂
/****
计算字符串长度
**/
#include<stdio.h>
void str(char *sum);
int main(void)
{
char a[]={"hello word"};
str(a);
return 0;
}
void str(char *sum)
{
int i;
while(*(sum+i) != 0){
i++;
}
printf("%d\n",i);
}
/**
复制字符串
*/
#include<stdio.h>
void strcpy(char *str,char *ptr);
int main(void)
{
char str[]={"hello world!"};
char ptr[]={"abcdef"};
strcpy(str,ptr);
printf("%s\n",str);
return 0;
}
void strcpy(char *str,char *ptr)
{
int i = 0;
while(ptr[i] != '\0'){
str[i]=ptr[i];
i++;
}
str[i] != '\0';
}
/*
比较字符串的大小
*/
#include<stdio.h>
int compare(char *str,char *ptr);
int main(void)
{
int i;
char str[]={"5"};
char ptr[]={"1"};
i = compare(str,ptr);
if(i==0){
printf("%s=%s\n",str,ptr);
}
if(i==1){
printf("%s>%s\n",str,ptr);
}
if(i==-1){
printf("%s<%s\n",str,ptr);
}
return 0;
}
int compare(char *str,char *ptr)
{
int i;
while(*(str+i) != '\0' && *(ptr+i) != '\0')
{
if(*str>*ptr){
return 1;
}else if(*str<*ptr){
return -1;
}
i++;
}
if(str != '\0'){
return 1;
}
if(ptr != '\0'){
return -1;
}
return 0;
}
/*
两个字符串相加
*/
#include<stdio.h>
char strcat(char *str,char *str1);
int main(void)
{
char str[]={"hello"};
char str1[]={"world"};
strcat(str,str1);
printf("%s\n",str);
return 0;
}
char strcat(char *str,char *str1)
{
int i=0;
while(str[i] != '\0'){
i++;
}
int j=0;
while(str1[j]!='\0'){
str[i++] = str1[j++];
}
str[i] = '\0';
}