Home > Net >  C language finding T or t in the entered word
C language finding T or t in the entered word

Time:01-24

You are interested in finding words that contain the letter 't' or 'T' in the first half of the word (including the middle letter if there is one). Specifically, if the first half of the word does contain a 't' or a 'T', your program should output a 1. If the first half does not contain the letter 't' or 'T', but the second half does, then your program should output a 2. Otherwise, if there is no 't' or 'T' in the word at all, your program's output should be -1. You may assume that the word entered does not have more than 50 letters.

 #include <stdio.h>
int main (void){
    int i=0;
    char word[51];
    int l=0;
    int half;
    char found1='T';
    char found2='t';
    
    
    scanf("%s",word);
    while (word[i]!='\0'){
        i  ;
        
    }
    half=i/2;
    while(word[l]!='\0'){
         scanf("%s",word);
         l  ;
        if((word[l]!=half)&&((word[l]==found1)&&(word[l]==found2))){
            printf("1");
        }
    if((word[l]!=i)&&((word[l]==found1)&&(word[l]==found2))&&(word[l]>=half)){
        printf("2");
    }
    }
    if(i%2!=0){
        printf("1");
    }else{
        printf("-1");
    }
    return 0;
}

CodePudding user response:

Break it down as simple as possible:


Find the index of a T or t in the string.

Did you find it?

  • no, output 0 and quit

Did you find it in the first half?

  • no, output 2 and quit

Output 1 and quit.


Get input with:

char word[52];
fgets(word, sizeof(word), stdin);

Determine the length of a string with:

int n = strlen(word) - 1;  // Don’t count the '\n' at the end of the input

Remember that integer division rounds toward zero, so:

int half = (n   1) / 2;

Anything < half is in the first half. Anything half is in the second half.

CodePudding user response:

#include <stdio.h>
#include <string.h>

int main() {
    char word[100];
    int i, found = 0;

    printf("Enter a word: ");
    scanf("%s", word); // get the word from the user

    for (i = 0; i < strlen(word); i  ) {
        if (word[i] == 't' || word[i] == 'T') {
            found = 1;
            break;
        }
    }

    if (found) {
        printf("The letter 't' or 'T' was found in the word.\n");
    } else {
        printf("The letter 't' or 'T' was not found in the word.\n");
    }

    return 0;
}

In this example, the program prompts the user to enter a word, which is stored in a char array called "word". Then, the program uses a for loop to iterate through each character of the word. Inside the loop, it checks if the current character is 't' or 'T' by using an if statement. If the letter 't' or 'T' is found, the variable "found" is set to 1 and the loop is broken. After the loop, the program checks the value of the "found" variable to determine whether the letter 't' or 'T' was found in the word or not.

It's worth noting that using scanf("%s", word) to get the input word from the user is unsafe, as if the user inputs a word longer than the size of the char array, it can cause a buffer overflow which may lead to a security vulnerability. It's a good practice to use fgets or getline instead.

  • Related