• C语言(字符串,DEVFORGE学编程社区)


    1、字符串左中右

     1 #include<stdio.h>
     2 #include <string.h>
     3 #define N 100
     4 
     5 void Left(char src[], int n, char dest[]);
     6 void Right(char src[], int n, char dest[]);
     7 void Mid(char src[], int loc, int n, char dest[]);
     8 int main()
     9 {
    10     int n,loc;
    11     char src[N] = "";
    12     char dest[N] = "";
    13     scanf("%s",src);
    14     scanf("%d%d",&n,&loc);
    15     
    16     Left(src,n,dest);
    17     Right(src,n,dest);
    18     Mid(src,loc,n,dest);
    19     return 0;
    20 }
    21 void Left(char src[], int n, char dest[]){    
    22     strncpy(dest,src,n);
    23     printf("%s
    ",dest);
    24 }
    25 void Right(char src[], int n, char dest[]){
    26     int len = strlen(src);
    27     strcpy(dest,&src[len-n]);
    28     printf("%s
    ",dest);
    29 }
    30 void Mid(char src[], int loc, int n, char dest[]){
    31     strncpy(dest,&src[loc],n);
    32     printf("%s
    ",dest);
    33 }

     2、分离单词

     1 #include<stdio.h>
     2 #include <string.h>
     3 #define N 256
     4 
     5 int main()
     6 {
     7     char str[N] = "";
     8     char s[N] = "";
     9     int count = 0;
    10     gets(str);
    11     
    12     /* 从字符串str尾部逐个截取单词连接到字符串s*/    
    13     for(char *p = str + strlen(str)-1; p>=str-1; p--)
    14     {
    15         if(*p>='a'&&*p<='z'||*p>='A'&&*p<='Z'){
    16             count++;
    17         }
    18         else{    
    19             if(*(p+1)>='a'&&*(p+1)<='z'||*(p+1)>='A'&&*(p+1)<='Z'){
    20                 strncat(s,(p+1),count);
    21                 strcat(s," ");                
    22             }                        
    23             count=0;
    24         }    
    25     }
    26     
    27     puts(s);/* 输出 */
    28     
    29     return 0;
    30 }

     3、删除字符

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include <string.h>
     4 #define N 100
     5 void deletechar(char s[],char c);
     6 int main()
     7 {
     8     char s[N] = "", c;
     9     gets(s);
    10     c = getchar();
    11     deletechar(s,c);
    12     puts(s);
    13     return 0;
    14 }
    15 void deletechar(char s[],char c){
    16     char t[N] = "";
    17     char *p = s, *q = t;
    18     while(*p)
    19     {
    20         if(*p!=c)
    21             *q++ = *p;
    22         p++;
    23     }
    24     strcpy(s,t);
    25 }

    4、字符串复制

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include <string.h>
     4 #define N 100
     5 void copy(char s1[],char s2[], int m);
     6 int main()
     7 {
     8     char s1[N] = "", s2[N] = "";
     9     int m;    
    10     gets(s1);
    11     scanf("%d",&m);
    12         
    13     copy(s1,s2,m);
    14     puts(s2);
    15     return 0;
    16 }
    17 void copy(char s1[],char s2[], int m){
    18     strcpy(s2,&s1[m]);
    19 }

    5、Music Composer

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include <string.h>
     4 #define N 100
     5 
     6 int main()
     7 {
     8     char key[10][3] = {"A#","Bb","C#","Db","D#","Eb","F#","Gb", "G#", "Ab"};
     9     char s1[N] = "", s2[N] = "";
    10     scanf("%s%s",s1,s2);
    11     
    12     if(strlen(s1)==1)
    13         strcpy(s1,"UNIQUE");
    14     else{
    15         for(int i=0; i<10; ++i)
    16         {    
    17             if(!strcmp(s1,key[i])){            
    18                 if(i%2)  i--;            
    19                 strcpy(s1,key[i]);
    20                 break;                
    21             }
    22         }
    23         puts(strcat(strcat(s1," "),s2));
    24     }    
    25     return 0;
    26 }

    6、字符串逆序

     1 #include<stdio.h>
     2 #define N 100
     3 /* 不让用字符串处理函数 */
     4 int main()
     5 {
     6     char s[N] = "";
     7     gets(s);
     8     /* 计算字符串长度 */
     9     int i;
    10     for(i=0; s[i]!='='; ++i);
    11     /* 去掉字符串中的=字符 */
    12     char *p = s, *q = s + i;
    13     *q-- = '';
    14     /* 逆序 */
    15     while(p<q)
    16     {
    17         char c = *p;
    18         *p = *q;
    19         *q = c;
    20         p++,q--;
    21     }
    22     /* 输出 */
    23     puts(s);
    24     return 0;
    25 }

    7、字符串排序

     1 #include<stdio.h>
     2 #include <string.h>
     3 #define N 10
     4 void QuickSort(char a[][N],int n, int left, int right);
     5 int main()
     6 {
     7     char str[N][N] = {""};
     8     for(int i=0; i<10; ++i)
     9         scanf("%s",str[i]);    
    10     QuickSort(str,10,0,9);
    11     for(int j=0; j<10; ++j)
    12         printf("%s ",str[j]);
    13     return 0;
    14 }
    15 /*快速排序*/
    16 void QuickSort(char a[][N],int n, int left, int right)
    17 {
    18     int i,j; 
    19     char t[10] = "";       
    20     if(left<right){
    21         i=left,j=right+1;/*左右指针*/
    22         while(i<j){
    23             while(i+1<n && (-1 == strcmp(a[++i],a[left])));
    24             while(j-1>-1 && (1 == strcmp(a[--j],a[left])));
    25             if(i<j)
    26             {strcpy(t,a[i]);strcpy(a[i],a[j]);strcpy(a[j],t); }            
    27         }
    28         strcpy(t,a[left]);strcpy(a[left],a[j]);strcpy(a[j],t);
    29         QuickSort(a,n,left,j-1);
    30         QuickSort(a,n,j+1,right);
    31     }
    32 }

    8、字符串替换

     1 #include<stdio.h>
     2 #include <string.h>
     3 #define N 1000
     4 
     5 void ReplaceAll(char *str, const char *you, const char *we);
     6 int main()
     7 {
     8     char str[N] = "";
     9     gets(str);
    10     ReplaceAll(str,"you","we");
    11     puts(str);
    12     return 0;
    13 }
    14 
    15 void ReplaceAll(char *str, const char *you, const char *we)
    16 {
    17     char temp[N] = "";
    18     char *prv = str;
    19     char *pos = strstr(str, you);
    20     while (pos) {
    21         strncat(temp, prv, pos - prv);/*字符串不含you的字符连接到temp*/
    22         pos += strlen(you);
    23         prv = pos;/*指针后移,忽略字符串you*/
    24         strcat(temp, we);/*字符串we连接到temp*/
    25         pos = strstr(prv, you); /*继续寻找字符串you*/      
    26     }
    27     strcat(temp,prv);/*连接剩余字符*/
    28     strcpy(str,temp);/*拷贝回字符串str*/
    29 }

    9、Coin  Test

     1 #include<stdio.h>
     2 #include <math.h>
     3 #define N 256
     4 
     5 void fun(int u, int sum);
     6 int main()
     7 {
     8     int u = 0, d = 0, s = 0;
     9     char str[N] = "";
    10     gets(str);
    11     char *p = str;
    12     
    13     while(*p)
    14     {
    15         if(*p=='U') u++;
    16         if(*p== 'D') d++;
    17         if(*p=='S') s++;
    18         p++;
    19     }
    20     
    21     int sum = u + d;
    22     double m = (double)u/sum;
    23     if(s) printf("WA");
    24     else if(fabs(m-0.5)>0.003) printf("Fail
    ");  
    25     else if(fabs(m-0.5)<=0.003) fun(u,sum);
    26     return 0;
    27 }
    28 void fun(int u, int sum)
    29 {
    30     int t = u>sum?u:sum;
    31     while(t)
    32     {
    33         if(u%t==0 && sum%t==0){
    34             u /= t;
    35             sum /= t;
    36             break;
    37         }        
    38         t--;
    39     }
    40     printf("%d/%d",u,sum);
    41 }

    10、特殊要求的字符串

     1 #include<stdio.h>
     2 #include <string.h>
     3 #define N 256
     4 
     5 void sortUp(char *s,int n);
     6 void sortDown(char *s,int n);
     7 int main()
     8 {
     9     char s[N] = "";
    10     char t[N] = "";
    11     gets(s);
    12     /* 1. */
    13     int len = strlen(s);
    14     int midIndex = len/2;
    15     if(len%2)
    16         midIndex++;
    17     /* 2. */
    18     strncpy(t,s,len/2);/* 保存前半部 */
    19     sortUp(&s[midIndex],len/2);/* 升序后半部 */
    20     strncpy(s,&s[midIndex],len/2);/* 有序后半部复制到前半部 */
    21     sortDown(t,len/2);/* 降序前半部 */
    22     strncpy(&s[midIndex],t,len/2);/* 有序前半部复制到后半部 */
    23     /* 3 */
    24     puts(s);
    25     return 0;
    26 }
    27 
    28 void sortUp(char *s,int n){
    29     char temp[N] = "";
    30     for(int i=0; i<n; ++i)    
    31         temp[s[i]]++;
    32     //升序
    33     int index = 0;
    34     for(int j=0; j<N; ++j)
    35     {
    36         while(temp[j])
    37         {
    38             s[index++] = j;
    39             temp[j]--;
    40         }
    41     }
    42     
    43 }
    44 
    45 void sortDown(char *s,int n){
    46     char temp[N] = "";
    47     for(int i=0; i<n; ++i)    
    48         temp[s[i]]++;
    49     //降序
    50     int index = 0;
    51     for(int j=N-1; j>=0; --j)
    52     {
    53         while(temp[j])
    54         {
    55             s[index++] = j;
    56             temp[j]--;
    57         }
    58     }    
    59 }
  • 相关阅读:
    【环境巡检】使用jmeter+ant+Jenkins+企业微信自动化巡检_测试报告中有接口失败才通知(4)
    【环境巡检】使用jmeter+ant+Jenkins+企业微信自动化巡检_集成Jenkins定时发送企业微信+邮件(3)
    【环境巡检】使用jmeter+ant+Jenkins+企业微信自动化巡检_ant调用jmeter脚本并生成报告(2)
    【CI/CD】Jenkins查询及自定义工作空间(自由风格项目、maven项目)
    【环境巡检】使用jmeter+ant+Jenkins+企业微信自动化巡检_jmeter实现脚本及响应超时、失败重试(1)
    day 18 面向对象-类与类之间的关系
    day 17 面向对象-成员
    day16 面向对象
    day14 内置函数2
    day013 内置函数
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/11637400.html
Copyright © 2020-2023  润新知