• C语言:在字符串中删除与某字符相同的字符


    用字符指针作函数参数编程实现如下功能:在字符串中删除与某字符相同的字符。
    **输入格式要求:"%s"  
    输入提示信息:
    "Input a string:"  
    "Input a character:"
    **输出格式要求:"Results:%s
    "
    程序运行示例1如下:
    Input a string:hello,world!
    Input a character:o
    Results:hell,wrld!
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define N 100
     4 void  Squeeze(char *s, char c);
     5 int main()
     6 {
     7     char  str[20], ch;
     8     printf("Input a string:");
     9     gets(str);
    10     printf("Input a character:");
    11     ch = getchar();
    12     Squeeze(str,ch);
    13     printf("Results:%s
    ", str);
    14     return 0;
    15 }
    16 void  Squeeze(char *s, char c)
    17 {
    18     int i,j,len;
    19     len = strlen(s);
    20     for (i = len; i>=0; i--)
    21     {
    22         if (s[i] == c)
    23         {
    24             for (j = i; j < len; j++)
    25             {
    26                 s[j] = s[j + 1];
    27             }
    28         }
    29     }
    30 }
    做法一
    
    
     1 #include <stdio.h>
     2 #include <string.h>
     3 #define N 100
     4 void  Squeeze(char *s, char c);
     5 int main()
     6 {                      
     7     char  str[20], ch;
     8     printf("Input a string:");
     9     gets(str);
    10     printf("Input a character:");
    11     ch = getchar();
    12     Squeeze(str, ch);
    13     printf("Results:%s
    ", str);
    14     return 0;
    15 }                      
    16 void  Squeeze(char *s, char c)
    17 {                      
    18     char str[N];
    19     char *t = str;
    20     strcpy(t, s);
    21     for (; *t != ''; t++)
    22     {                      
    23         if (*t != c)
    24         {                      
    25             *s = *t;
    26             s++;
    27         }
    28     }
    29     *s = '';  /* 在字符串t2的末尾添加字符串结束标志 */
    30 }          
    做法二
     
  • 相关阅读:
    HTML5_音视频标签 <audio> 和 <video>
    HTML5_提供的 新功能_less 编译_
    HTML5_新标签
    CSS3_综合案例
    CSS3_元素拖曳原理_设置全局点击捕获_九宫格碰撞检测_自定义滚动条
    CSS3_移动端_开机动画
    CSS3_动画 animation
    剑指Offer-2.替换空格(C++/Java)
    MySQL学习笔记4——DQL
    MySQL学习笔记3——DCL
  • 原文地址:https://www.cnblogs.com/20201212ycy/p/14815024.html
Copyright © 2020-2023  润新知