int strlen(const char *s){
int len = 0;
while(*s++) len++;
return len;
}
void strcpy(char *d, const char *s){
while(*d++ = *s++);
}
void strncpy(char *s1, const char *s2, size_t n){
char *tmp = s1;
int len = strlen(s1);
while(n){
*s1++ = *s2++;
n--;
}
*s1++ = '\0';
}
void strcat(char *s1, const char *s2){
while(*s1) *s1++;
while(*s1++ = * s2++);
}
void strncat(char *s1, const char *s2, size_t n){
while(*s1) *s1++;
while(n--) *s1++ = * s2++;
*s1++ = '\0';
}
int strcmp(const char *s1, const char *s2){
while(*s1 == *s2){
if(*s1 == '\0') return 0;
s1++;
s2++;
}
return (unsigned char)*s1 - (unsigned char)*s2;
}
int strncmp(const char *s1, const char *s2, size_t n){
while(n && *s1 && *s2){
if(*s1 != *s2) return (unsigned char)*s1 - (unsigned char)*s2;
s1++;
s2++;
n--;
}
if(!n) return 0;
if(*s1) return 1;
return -1;
}
int main(){
char str1[256] = "abcde";
char str2[256] = "12345";
char str3[256] = "AaBbCcDdEe";
int n = 3;
printf("str1:%s\n", str1);
printf("str2:%s\n", str2);
printf("str1の長さは%dです\n", strlen(str1));
printf("str2をstr1にコピーします\n");
strcpy(str1, str2);
printf("str1:%s\n", str1);
printf("str2の先頭%d文字をstr1にコピーします\n", n);
strncpy(str1, str2, n);
printf("str1:%s\n", str1);
printf("str2をstr1と連結します\n");
strcat(str1, str2);
printf("str1:%s\n", str1);
printf("str2の先頭%d文字をstr1に連結します\n", n);
strncat(str1, str2, n);
printf("str1:%s\n", str1);
printf("str2とstr1を比較します\n");
printf("str1 : str2 ? %d\n", strcmp(str1, str2));
printf("str2とstr1の先頭%d文字を比較します\n", n);
printf("str1 : str2 ? %d\n", strncmp(str1, str2, n));
return 0;
}
最初実行してみたらが無いとのことなので追記して実行してみたのですが、以下のようなエラー文が出ました。解決お願いします。
返信削除w8_2.c:4:5: error: conflicting types for ‘strlen’
4 | int strlen(const char *s){
| ^~~~~~
In file included from w8_2.c:1:
/usr/include/string.h:385:15: note: previous declaration of ‘strlen’ was here
385 | extern size_t strlen (const char *__s)
| ^~~~~~
w8_2.c:13:6: error: conflicting types for ‘strcpy’
13 | void strcpy(char *d, const char *s){
| ^~~~~~
In file included from w8_2.c:1:
/usr/include/string.h:122:14: note: previous declaration of ‘strcpy’ was here
122 | extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
| ^~~~~~
w8_2.c:18:6: error: conflicting types for ‘strncpy’
18 | void strncpy(char *s1, const char *s2, size_t n){
| ^~~~~~~
In file included from w8_2.c:1:
/usr/include/string.h:125:14: note: previous declaration of ‘strncpy’ was here
125 | extern char *strncpy (char *__restrict __dest,
| ^~~~~~~
w8_2.c:32:6: error: conflicting types for ‘strcat’
32 | void strcat(char *s1, const char *s2){
| ^~~~~~
In file included from w8_2.c:1:
/usr/include/string.h:130:14: note: previous declaration of ‘strcat’ was here
130 | extern char *strcat (char *__restrict __dest, const char *__restrict __src)
| ^~~~~~
w8_2.c:38:6: error: conflicting types for ‘strncat’
38 | void strncat(char *s1, const char *s2, size_t n){
| ^~~~~~~
In file included from w8_2.c:1:
/usr/include/string.h:133:14: note: previous declaration of ‘strncat’ was here
133 | extern char *strncat (char *__restrict __dest, const char *__restrict __src,
| ^~~~~~~
追記です。
削除最初の文で、string.h が無かったので入れてから実行しました。