c/c++语言开发共享删除字符串的字符

我正在尝试编写一个代码,要求用户输入一个字符串并除了按字母顺序排列所有字符。

现在我自己做了,似乎没有正常工作。 我是字符串的新手,所以我正在努力理解和掌握字符串。 我试图在Mac上使用gdb,但我没有所有的function来理解这一点。 能否请你帮忙?

代码必须做什么:用户输入(例如): h**#el(l)o&^w

输出是hello.

这是我的代码:

 #include  #include  int main() { char string[100]; int i; int seen = 0; printf("Enter String: "); scanf("%s", string); for (i=0; string[i]!=''; i++) { if (((string[i]'z')&&(string[i]'Z')) ||string[i]!='') { seen = 1; } else seen = 0; } if (seen==0) { printf("%s", string); } } 

    好吧,你的代码有几个重要的问题:

    所以基本上,你想要的是使用fgets()而不是scanf()

    但是为什么不只是按字符获取输入字符,并构建一个只有你想要的字符的字符串? 它更简单灵活!

    基本上:

     #include  int main() { char* string[100]; int i=0; printf("Enter your string: "); do { // getting a character char c = getchar(); // if the character is alpha if (isalpha(c) != 0) // we place the character to the current position and then increment the index string[i++] = c; // otherwise if c is a carriage return else if (c == 'r') { c = getchar(); // get rid of n // we end the string string[i] = '' }else if (c == 'n') // we end the string string[i] = ''; // while c is not a carriage return or i is not out of boundaries } while (c != 'n' || i < 100); // if we've got to the boundary, replace last character with end of string if (i == 100) string[i] = ''; // print out! printf("Here's your stripped string: %sn", string); return 0; } 

    我没有在我的电脑上运行它,因为它已经迟到了,所以我在出错的时候道歉。

    附录:

    程序会跳过我的陈述并关闭

    这是因为你的条件是反转的,并删除条件,因为它总是会发生在scanf()总是将附加到字符串以结束它。 尝试交换seen = 1seen = 0或尝试使用以下条件:

      if ((string[i]>='a' && string[i]<='z')||(string[i]>='A' && string[i]<='Z'))) seen = 1; else seen = 0; 

    或者简单地说,使用ctypesisalpha()函数,就像在我们的两个例子中一样!

    没有任何部分(删除多余的字符)来更改代码中的字符串。

     #include  #include  #include  char *filter(char *string, int (*test)(int)) { char *from, *to; for(to = from = string;*from;++from){ if(test(*from)) *to++ = *from; } *to = ''; return string; } int main(){ char string[100]; printf("Enter String: "); scanf("%99s", string); printf("%sn", filter(string, isalpha)); return 0; } 

      以上就是c/c++开发分享删除字符串的字符相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/519551.html

      (0)
      上一篇 2020年12月5日
      下一篇 2020年12月5日

      精彩推荐