【问题标题】:Removing Punctuations in a string in C using Switch case使用 Switch case 删除 C 中字符串中的标点符号
【发布时间】:2018-07-30 18:40:56
【问题描述】:

有人可以帮我看看我的代码有什么问题吗?我使用 switch case 制作了解决方案,并将标点符号替换为空字符串。

#include<stdio.h>
#include<string.h>
int main() 
{
    char st[50];
    int i;
    printf("ENter the string:\n");       
    gets(st);
    for(i=0;i<strlen(st);i++)
    {
        switch(st[i])
        {
            case '!':
            case '"':
            case '#':
            case '$':
            case '%':
            case '&':strcpy(st[i]," ");
                     break;
        }
        printf("String is:\n");
        puts(st);
    }
    return 0;
}

【问题讨论】:

  • 为什么要对单个字符使用strcpy
  • case else 和 switch 的结束呢?为什么不使用 OR 逻辑,这很短可能是一个 If 语句。我的 C++ 生锈了,一直在研究 C# 及其不同之处。
  • strcpy 会将空白和 NULL 终止符复制到 src 指定的位置,在您的情况下,这甚至不是一个好的地址。你应该改变:strcpy(st[i]," "); => st[i] = ' ';.. 你的开关应该有一个默认情况,它只是中断。此外,编译时打开警告,因为它们会通知您您的错误。
  • 函数:gets() 已经贬值多年,并在 C 标准 C11 中完全从 C 语言中删除

标签: c arrays string loops switch-statement


【解决方案1】:
  1. strcpy(st[i]," ") 错误使用st[i]=' '(strcpy是用来复制字符串的,是单字符直接赋值的过程)
  2. gets(st) 现在从 C 中删除。它会导致 buffer overflows 。使用fgets()Read more on gets() and fgets()

这里可以用fgets()替换gets():-

fgets(st,50,stdin);

修改后的代码:-

#include <stdio.h>
#include <string.h>
int main()
{
    char st[50];
    int i;
    printf("ENter the string:\n");
    fgets(st, 50, stdin);
    for (i = 0; i < strlen(st); i++)
    {
        switch (st[i])
        {
        case '!':
        case '"':
        case '#':
        case '$':
        case '%':
        case '&':
            st[i] = ' ';
            break;
        }
        printf("String is:\n");
        puts(st);
    }
    return 0;
}

推荐:- 将 puts() 移到 for-loop 之外。

输出:-

ENter the string:
!hello#%worl$
String is:
 hello#%worl$

String is:
 hello#%worl$

String is:
 hello#%worl$

String is:
 hello#%worl$

String is:
 hello#%worl$

String is:
 hello#%worl$

String is:
 hello %worl$

String is:
 hello  worl$

String is:
 hello  worl$

String is:
 hello  worl$

String is:
 hello  worl$

String is:
 hello  worl$

String is:
 hello  worl 

String is:
 hello  worl 

【讨论】:

  • for (i = 0; i &lt; strlen(st); i++) 将调用strlen() N 次,每次调用需要 N 次迭代 - 使此代码为 O(N*N)。考虑for (i = 0; st[i]; i++)。输出打印时间仍然超过任何此类改进。
  • PS- 你有一个错字我无法修正,因为它少于六个字符。您说“fegts()”而不是“fgets()”——“gets(st) 现在已从 C 中删除。它会导致缓冲区溢出。使用 fegts()。阅读有关 gets() 和 fgets() 的更多信息”跨度>
  • @torstenvl 感谢您的通知。我变了。
【解决方案2】:

" " 是一个空格。我认为这不是您的预期行为。使用另一个缓冲区进行复制。例如,

 #include<stdio.h>
        #include<string.h>
        int main() 
        {
            char sta[50];
            char stb[50];
            int i,j;
            printf("ENter the string:\n");       
            gets(sta);
            for(i=0,j=0;i<strlen(sta);i++)
            {
                switch(st[sta])
                {
                    case '!':
                    case '"':
                    case '#':
                    case '$':
                    case '%':
                    case '&': break;
                    default: 
                             stb[j++]=sta[i];
                             break;
                }
                stb[j] = (char)0; // C str termination...
                printf("String is:\n");
                puts(stb);
            }
            return 0;
        }

【讨论】:

  • 关闭。 ' ' 是一个空格" " 是一个字符串文字
  • 这个答案使用标点符号而不是字母构建stb[]
  • 对不起,我错过了一些东西。编辑。
【解决方案3】:

以下建议代码:

  1. 干净编译
  2. 消除“神奇”数字
  3. 用空格正确替换列出的标点符号
  4. 只打印一次结果
  5. 使用“有效”函数fgets() 而不是(当前)不存在的gets() 函数
  6. 避免比较有符号和无符号值
  7. 限制变量i的范围

现在,建议的代码:

#include<stdio.h>
#include<string.h>

#define MAX_STR_LEN 50

int main( void ) 
{
    char st[ MAX_STR_LEN + 1 ];

    printf("ENter the string, max 50 characters\n");       
    fgets( st, sizeof( st ), stdin );

    for( size_t i=0;i<strlen(st);i++)
    {
        switch(st[i])
        {
            case '!':
            case '"':
            case '#':
            case '$':
            case '%':
            case '&':
                st[i] = ' ';
                break;

            default:
                break;
        }
    }

    printf( "Modified String is:\n %s\n", st );
    return 0;
}

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多