Ryan
Ryan

Reputation: 57

Switch Case help C

I am attempting to check string letters against another let of letters, checking the first letter in userword[k] against all the letters in letterstest[t] and if they match switching that matching letter of letterstest[t] with 0 so it cannot be matched again. Where I am confused on is inside the switch(){ and what exactly would work. Is there a case: what can switch the letters of the strings?

for (k = 0; k<wordsize; k++){
    for(t=0; t<8, t++){
        if (userword[k] != letterstest[t]) 
            return 0;

        if (userword[k] == letterstest[t]){    
            switch (letterstest[t]){

                    //unsure what case would work here
            }
        }
    }
}

Upvotes: 0

Views: 134

Answers (2)

ruakh
ruakh

Reputation: 183311

I think you're misunderstanding what switch is. switch is a selection structure, like if/else. For example, these two code-snippets are (generally) equivalent:

if(a == 0)
    printf("%s\n", "zero");
else if(a == 1)
    printf("%s\n", "one");
else if(a == 2)
    printf("%s\n", "two");
else
    printf("%s\n", "invalid");

switch(a)
{
    case 0:
        printf("%s\n", "zero");
        break;
    case 1:
        printf("%s\n", "one");
        break;
    case 2:
        printf("%s\n", "two");
        break;
    default:
        printf("%s\n", "invalid");
}

I'm not completely clear on what you're trying to do, but when you write "if they match switching that matching letter of letterstest[t] with 0 so it cannot be matched again", it sounds like you mean this:

    if (userword[k] == letterstest[t]){
        letterstest[t] = '\0';
    }

Edited to add: O.K., I think I now understand what you're trying to do:

  • you want to confirm that every character between userword[0] and userword[wordsize-1] appears somewhere between letterstest[0] and letterstest[7].
  • if a given character appears multiple times between userword[0] and userword[wordsize-1], then it must appear at least as many times between letterstest[0] and letterstest[7]. That is — a character between letterstest[0] and letterstest[7] can only count once.
  • you're O.K. with changing what characters appear between letterstest[0] and letterstest[7], as long as the final answer is correct; that is, you don't need to preserve the contents of letterstest.
  • the character '\0' does not occur anywhere between userword[0] and userword[wordsize-1], so can be used as a "dummy" value meaning "not a match".

Is that correct?

If so, then you can write:

for(k = 0; k < wordsize; k++) {
    for(t = 0; t < 8; t++) {
        if(userword[k] == letterstest[t]) {
            letterstest[t] = '\0'; /* don't let letterstest[t] count again */
            break; /* O.K., we've matched userword[k], we can move on */
        }
    }
    if(t == 8) /* we reached letterstest[8] without finding a match */
        return 0;
}
return 1; /* we found a match for each character */

Upvotes: 5

user799518
user799518

Reputation:

Ok first you can use any functions to match character or string in other... (If you want not use "for" loops)

In linux use "man" command on (functions):

  • strchr
  • strstr
  • index
  • rindex ...

After if you want switch a char you can use the ascii code of char (http://www.table-ascii.com/), or directly the char...

Example if you want check the 'A' character you can make that:

// Char Method
switch (letterstest[t]){
    case 'A':
        printf("A Detected !\n");
        break;
}

// Or Use The ASCII Code Method
switch (letterstest[t]){
    case 65:
        printf("A Detected !\n");
        break;
}

Example if you want check the '0' character (the number) you can make that:

// Char Method
switch (letterstest[t]){
    case '0':
        printf("0 Detected !\n");
        break;
}

// Or Use The ASCII Code Method
switch (letterstest[t]){
    case 48:
        printf("0 Detected !\n");
        break;
}

Example if you want check the 0 character ('\0' NUL) you can make that:

// Char Method
switch (letterstest[t]){
    case '\0':
        printf("NUL Detected !\n");
        break;
}

// Or Use The ASCII Code Method
switch (letterstest[t]){
    case 0:
        printf("NUL Detected !\n");
        break;
}

Upvotes: 0

Related Questions