1)撰寫子函式fstrcat(char *s,char *t)完成字串處理函式strcat的功能。
2)撰寫子函式fstrcpy(char *s,char *t)完成字串處理函式strcpy的功能。
3)撰寫子函式fstrcmp(char *s,char *t)完成字串處理函式strcmp的功能。
4)撰寫子函式fstrlen(char *s)完成上述字串處理函式strlen的功能。
5)主函式功能:從鍵盤輸入字串,呼叫上述4個子函式,輸出子函式的回傳值或內容發生改變的字串。
uj5u.com熱心網友回復:
char* fstrcat(char *s,char *t)
{
int i=0;
char* temp = s;
char* temp2 = t;
while(*temp != '\0')
{
i++;
temp++;
}
while(*temp2)
{
s[i++]=*temp2;
temp2++;
}
s[i]='\0';
return s;
}
uj5u.com熱心網友回復:
供參考
#include <stdio.h>
#include <stdlib.h>
//字串拼接
char *fstrcat(char *s, char *t);
//字串拷貝
char *fstrcpy(char *s, char *t);
//字串比較
int fstrcmp(char *s, char *t);
//字串長度
int fstrlen(char *s);
int main()
{
int j, k;
char s1[100], s2[100], s3[100], s4[100];
char *p = s3;
char *q = s4;
printf("\n輸入第一個字串:");
scanf("%s", s1);
printf("\n輸入第二個字串:");
scanf("%s", s2);
p = fstrcat(s1, s2);
printf("\n拼接后的字串:%s\n", p);
q = fstrcpy(s1, s4);
printf("\n復制后字串:%s\n", q);
j = fstrcmp(s1, s2);
if ( j == 1)
printf("[%s]比[%s]大\n", s1, s2);
else if (j == 0)
printf("[%s]與[%s]相等\n", s1, s2);
else
printf("[%s]比[%s]小\n", s1, s2);
k = fstrlen(s1);
printf("[%s]的長度是: %d\n", s1, k);
return 0;
}
//字串拼接
char *fstrcat(char *s, char *t)
{
int i = 0, j = 0;
while ( s[i] != '\0')
{
i++;
}
j = i;
i = 0;
while (t[i] != '\0')
{
s[j++] = t[i++];
}
s[j] = '\0';
return s;
}
//字串拷貝
char *fstrcpy(char *s, char *t)
{
int i = 0;
while ( s[i] != '\0')
{
t[i] = s[i];
i++;
}
t[i] = '\0';
return t;
}
//字串比較
int fstrcmp(char *s, char *t)
{
int i = 0;
while (s[i] != '\0' && t[i] != '\0')
{
if (s[i] > t[i])
return 1;
else if (s[i] == t[i])
return 0;
else
return -1;
i++;
}
}
//字串長度
int fstrlen(char *s)
{
int i = 0;
while ( s[i] != '\0')
{
i++;
}
return i;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63073.html
標籤:C語言
上一篇:求解
下一篇:求解急用
