删除列的文字 [英] Removing Characters of a String

查看:99
本文介绍了删除列的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个code,要求用户输入一个字符串,并采取所有字符,除了字母。

现在我做到了我自己,它似乎并没有正常工作。我是新来的字符串所以我想了解和掌握字符串。我试图在Mac上使用gdb的,但我没有所有的功能明白这一点。
能否请你帮忙吗?

什么是code必须做到:用户输入(例如):ħ**#EL(L)的O& ^ W

和输出你好。

这是我的code:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;诠释的main()
{
   字符字符串[100];
   INT I;
   诠释看出= 0;   的printf(请输入字符串:);
   scanf函数(%S,字符串);   对于(i = 0;字符串[我] ='\\ 0';!我++)
   {
       如果(((字符串[1] - ='A'||字符串[I]>Z)及及(字符串[1] - ='A'||字符串[I]>' Z'))||字符串[I]!='\\ 0')
       {
           看出= 1;
       }
       其他
           看出= 0;
   }
   如果(看到== 0)
   {
       的printf(%S,字符串);
   }
}


解决方案

任何部分(除去多余的字符),以改变你code中的字符串。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&文件ctype.h GT;字符*过滤器(字符*字符串,INT(*测试)(INT)){
    字符*从*到;
    对于(从字符串= =; *从++从){
        如果(测试(* FROM))
            *为++ = * FROM;
    }
    *为='\\ 0';
    返回的字符串;
}诠释主(){
    字符字符串[100];
    的printf(请输入字符串:);
    scanf函数(%99秒,字符串);
    的printf(%S \\ n,过滤器(串,因而isalpha));    返回0;
}

I'm trying to write a code that asks the user to enter a string and takes of all characters except the alphabetical.

Now i did it myself and it doesn't seem to work properly. I'm new to strings so i'm trying to understand and master strings. I tried to use gdb on mac but i don't have all the functions to understand this. Could you please help?

What the code must do: User inputs (for example): h**#el(l)o&^w

and the output is hello.

here is my code:

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

int main()
{
   char string[100];
   int i;
   int seen = 0;

   printf("Enter String: ");
   scanf("%s", string);

   for (i=0; string[i]!='\0'; i++)
   {
       if (((string[i]<='a' || string[i]>'z')&&(string[i]<='A' || string[i]>'Z')) ||string[i]!='\0')
       {
           seen = 1;
       }
       else
           seen = 0;
   }
   if (seen==0)
   {
       printf("%s", string);
   }
}

解决方案

No part(remove the extra characters) to change the string in your code.

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

char *filter(char *string, int (*test)(int)) {
    char *from, *to;
    for(to = from = string;*from;++from){
        if(test(*from))
            *to++ = *from;
    }
    *to = '\0';
    return string;
}

int main(){
    char string[100];
    printf("Enter String: ");
    scanf("%99s", string);
    printf("%s\n", filter(string, isalpha));

    return 0;
}

这篇关于删除列的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆