#include <stdio.h>
#include <string.h>
int str_len(char *p);
int str_cmp(char *p, char*pp);
void str_cat(char *p, char *pp);
void mystr_swap(char *p, char *pp);
int main()
{
int i;
char string[20], comp1[20], comp2[20],cat1[20], cat2[20],swap1[20],swap2[20];
printf("文字列の長さを調べる>文字列の入力\n");
scanf("%s", string);
printf("文字列の長さは %d\n", str_len(string));
printf("二つの文字列の比較>");
scanf("%s%s", comp1, comp2);
if(str_cmp(comp1, comp2) > 0)printf("%s > %s\n",comp1, comp2);
if(str_cmp(comp1, comp2) < 0)printf("%s < %s\n",comp1, comp2);
if(str_cmp(comp1, comp2) == 0)printf("%s = %s\n",comp1, comp2);
printf("二つの文字列を繋ぐ\n");
scanf("%s%s", cat1, cat2);
str_cat(cat1, cat2);
printf("%s\n", cat1);
printf("交換します\n");
scanf("%s%s", swap1, swap2);
printf("交換前\n%s\n%s\n", swap1, swap2);
mystr_swap(swap1, swap2);
printf("交換後\n%s\n%s\n", swap1, swap2);
return 0;
}
int str_len(char *p)
{
int i;
for(i=0;*p!='\0';p++)
i++;
return i;
}
int str_cmp(char *p, char*pp)
{
for(;*p == *pp;p++,pp++){
if(*p == '\0')return 0;
}
return *p - *pp;
}
void str_cat(char *p, char *pp)
{
for(;*p != '\0';p++)
;
for(;*pp != '\0';p++,pp++){
*p = *pp;
}
*p='\0';
}
void mystr_swap(char *p, char *pp)
{
char tmp[20];
strcpy(tmp, p);
strcpy(p, pp);
strcpy(pp, tmp);
}