0

我一直在阅读一些关于 C 的东西,我是一个初学者,此时这就是我所处的位置:

此函数保留来自键盘的输入:

void store_sequence(char *arg) {
    strcpy(estado.seq, arg);
    estado.tamanho = strlen(arg);
}

这就是我到目前为止检查键盘上插入的字符串上是否有 As 的内容:

void sequence_does_contain_As_and_Bs(char *arg) {

  char buf [] = estado.seq;

  s = strchr (buf, 'A');

  if (s != NULL)
    printf ("found a 'A' at %s\n", s);

}

所以,基本上,我需要检测输入字符串是否只有 As 和 Bs

4

2 回答 2

2

看看这个 - http://www.cplusplus.com/reference/cstring/

特别是strspn()strcspn()

于 2013-03-15T17:26:55.780 回答
1

尝试这个:

char buf [] = estado.seq;
int len = estado.tamanho;
int i;
int contains = 1;
for (i = 0; i < len; ++i)
    if (buf[i] != 'A' && buf[i] != 'B') {
        contains = 0;
        break;
    }
if (contains)
  // do whatever you want if the string contains only As and Bs
于 2013-03-15T17:30:50.673 回答