字符串处理函数

#include<string.h>

长度 strlen(str)     string length

赋值 strcpy(str2,str1)     string copy

连接 strcat(str2,str1)     string combination

比较 strcmp(str1,str2)     string comparison → 比较ASCII码

问题:请编程实现按奥运会参赛国国名在字典中的顺序对其入场次序进行排序。假设参赛国不超过150个。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define MAX_LEN 10     /*字符串最大长度*/ 
 4 #define N       150    /*字符串个数*/ 
 5 void SortString(char str[][MAX_LEN],int n);
 6 int main(void)
 7 {
 8     int i,n;
 9     char name[N]{MAX_LEN};     /*定义二维字符数组*/ 
10     printf("How many countries?");
11     scanf("%d",&n);
12     getchar();                 /*读走输入缓冲区中的回车符*/ 
13     printf("Input their names:
");
14     for(i=0;i<n;i++)
15     {
16         gets(name[i]);         /*输入n个字符串*/
17     }
18     SortString(name,n);        /*字符串按字典顺序排序*/ 
19     printf("Sorted results:
");
20     for(i=0;i<n;i++)
21     {
22         puts(names[i]);
23     }
24     return 0;
25 }
26 
27 void SortString(char str[][MAX_LEN],int n)
28 {
29     int i,j;
30     char temp[MAX_LEN];
31     for(i=0;i<n-1;i++)
32     {
33         for(j=i+1;j<n;j++)
34         {
35             if(strcmp(str[j],str[i])<0)
36             {
37                 strcpy(temp,str[i]);
38                 strcpy(str[i],str[j]);
39                 strcpy(str[j],temp);
40             }
41         }
42     }
43 }
#include<stdio.h>
#define N 80
unsigned int MyStrlen(const char *pStr);
main()
{
    char str[N];
    printf("Input a string:");
    gets(str);
    printf("The length of the string is:%u
",MyStrlen(str));
}
unsigned int MyStrlen(const char *pStr)
{
    unsigned int len=0;
    for(;*pStr!='';pStr++)
    {
        len++;
    }
    return len;
}
MyStrlen(str)
 1 #include<stdio.h>
 2 #define N 80
 3 void  MyStrcpy(char dstStr[],char srcStr[]);
 4 main()
 5 {
 6     char str1[N],str2[N];
 7     printf("Input a string:");
 8     gets(str1);
 9     MyStrcpy(str2,str1);
10     printf("The copy is:");
11     puts(str2);
12 }
13 void  MyStrcpy(char dstStr[],char srcStr[])
14 {
15     int i=0;
16     while(srcStr[i]!='')
17     {
18         dstStr[i]=srcStr[i];
19         i++;
20     }
21     dstStr[i]='';
22 }
MyStrcpy(str2,str1)
 1 #include<stdio.h>
 2 #define N 80
 3 void  *MyStrcat(char *dstStr, char *srcStr);
 4 main()
 5 {
 6     char str1[2*N],str2[N];
 7     printf("Input a string:");
 8     gets(str1);
 9     printf("Input another string:");
10     gets(str2);
11     printf("Concatenate results:%s
",MyStrcat(str1,str2));
12 }
13 void  *MyStrcat(char *dstStr, char *srcStr)
14 {
15     char *pStr=dstStr;
16     while(*dstStr!='')
17     {
18         *dstStr++;
19     }
20     for(;*srcStr!='';dstStr++,srcStr++)
21     {
22         *dstStr=*srcStr;
23     }
24     *dstStr='';
25     return pStr;
26 }
MyStrcat(str1,str2)
 1 #include <stdio.h>
 2 #define SIZE 80
 3 int MyStrcmp(char s[],char t[]);
 4 main()
 5 {                        
 6     char s[SIZE],t[SIZE],i;
 7     printf("Input s
");
 8     gets(s);//1
 9     printf("Input t
");
10     gets(t);//1
11     i=MyStrcmp(s,t);//2
12     if(i>0)//1
13         printf("string s>string t.
");
14     else if(i<0)//1
15         printf("string s<string t.
");
16     else//1
17         printf("string s=string t.
");
18 }                        
19 int MyStrcmp(char s[], char t[])
20 {                        
21     int i;
22     for (i=0; s[i] == t[i]; i++ ) //6
23     {                        
24             if (s[i] ==  '' )  //2
25             return 0 ;//2
26     }
27     return (s[i]-t[i]); //2
28 }                       
MyStrcmp(str1,str2)
原文地址:https://www.cnblogs.com/20201212ycy/p/14778627.html