0

I've been reading some stuff about C, i'm a beginner and at this point this is where i'm at:

This function keeps the input from the keyboard:

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

and this is what i came so far to check if there are As on the string that was inserted on the keyboard:

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);

}

So, basically, i need to detect if the input string has only As and Bs

2 Answers 2

2

Have a look at this - http://www.cplusplus.com/reference/cstring/

Particularly strspn() and strcspn()

Sign up to request clarification or add additional context in comments.

2 Comments

THans SpacedMonkey, i came to this: ´int sequencia_nao_contem_so_As_e_Bs(char *arg){ int i; char cset[] = "CDEFGHIJLMNOPQRSTUVXYZcdefghijlmnopqrstuvxyz"; i = strspn (arg,cset); return ("%d",i); }´ and this doesn't allow me to put HGBGD and it's fine, because i want only "ABABABBABBBA" characters with A and B, but if i put HFDHDAB it accepts
if ( strspn(arg, "AB") == strlen(arg) ) /* only As and Bs */
1

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.