通过指针变量p去掉字符串中字母之间的星号

通过指针变量p去掉字符串中字母之间的星号

如题。

  • Sample Input
****A**B***CD*EF*****
  • Sample Output
****ABCDEF*****
  • 代码
#include <stdio.h>
#include <string.h>

int main(int argc, char const *argv[]) {
  char s[100] = "\0";
  char *p[100];
  int i = 0, l, end;
  scanf("%s", s);
  while (s[i] == '*') {
    p[i] = &s[i];
    i++;
  }/*指向字符串最开始的'*'号*/
  for (int j = i; j < strlen(s); j++) {
    if(s[j] != '*') {
      p[i++] = &s[j];/*读取字符串中字母*/
      end = j;/*记录最后一个字母位置*/
    }
  }
  for (int j = end + 1; j < strlen(s); j ++) {
    p[i++] = &s[j];/*指向字符串结束的'*'号*/
  }
  for (int j = 0; j < i; j++) {
    printf("%c", *p[j]);
  }
  return 0;
}

陕西科技大学 C语言程序设计课作业 指针-20190327 第三题

猜你喜欢

转载自blog.csdn.net/weixin_44413445/article/details/89363731