1

I'm writing a code that must identify the letter 't' or 'T' in a word, before or after the middle of it. If the first half of the word does contain a 't' or a 'T', the program should output a 1. If the first half does not contain the letter 't' or 'T', but the second half does, then the program should output a 2. Otherwise, if there is no 't' or 'T' in the word at all, the program's output should be -1. The word entered will not have more than 50 letters.

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

int main() {
    char word[50];
    int i = 0, length, t = 0, T = 0;

    scanf("%s", word);

    length = strlen(word);
    t = word[i] == 't';
    T = word[i] == 'T';

    while(!t || !T) {
        if((t || T) && i <= length / 2) {
            printf("%d", '1');
        } else if((t || T) && i > length / 2) {
            printf("%d", '2');
        
        }
        i++;
    }

    return 0;
}

If I enter any word and press enter, nothing is printed. Another thing is that when I remove the comment slashes from the two lines at the bottom, the program goes through an infinite loop.

What makes the program go into an infinite loop?

3
  • 2
    Stepping through the code in a debugger should show you what's happening. Have you tried doing so?
    – Ken White
    Commented Jan 8, 2022 at 2:42
  • 2
    Assigning t = word[i] == 't'; assigns a value to t based on the value i has only at that moment. It does not bind t to an expression that is reevaluated when i changes. This shows you have not learned how C works and are not ready for this problem. Go back to earlier lessons. Commented Jan 8, 2022 at 3:02
  • 1
    You don't change either t or T inside the loop, so your loop is going to either run not at all or run forever. Neither is what you intend, I assume. You should probably check for overruns of i in the loop control: while ((!t || !T) && i < length), and you probably need the assignments to t and T moved inside the loop. Commented Jan 8, 2022 at 3:58

2 Answers 2

1

This sounds like a school assignment, so I'll focus on advising/critiquing your code rather than giving a solution.

The first recommendation I have is to use a for loop instead of a while loop. A Rule of thumb in C is to only use a while loop when you actually don't have any idea how many things you need your program to look at.

You already have the length of the string, so set up your for loop to loop exactly once for each character.

Next you need to change how you are using printf. The %d format specifier is for printing integers, but you are passing it '1'. This is not an integer, it is the ascii representation of the symbol 1 (which is actually has the value 49, see the ascii table for more info)

You can either pass printf the value 1, or use the %c specifier, which expects ascii characters.

Better yet, just say printf("1");

That doesn't get you all the way there, but I think it lays the ground work so you can find the solution!

1

Condition !t || !T has no sense to be used as loop condition ...ask yourself how the loop will end ? you need just to check i is less than length

Second, the assignments t = word[i] == 't'; T = word[i] == 'T'; outside the loop have no sense ...you will be just pointing to the zero index of the string ...you should check all characters

third , the printf lines need to use %d

fourth , you appear not getting the purpose of the program printing inside loop will lead to printing many numbers and you just want to know if there is t or T you need to print single line.you may use variable int result=0; to hold the value you want and print it in the end ...of course you will need using break statement in the if((t || T) && i <= length / 2) and if((t || T) && i > length / 2) because no need for more searching

fifth, you should re-read , re-think , re-code the assignment before going bored and asking about it

sixth, there is a working version by modifying your code but you should try writing a good solution before looking at a solution as it better to solve your problems by yourself

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

int main() {
    char word[50];
    int i = 0, length, t = 0, T = 0;

    scanf("%s", word);

    length = strlen(word);
    int result=0;

    while( i<length) {
        t = word[i] == 't';
        T = word[i] == 'T';
        if((t || T) && i <= length / 2) {
            result=1;
            break;
        } else if((t || T) && i > length / 2) {
            result=2;
            break;
        }else{
            result=-1;
        }
        i++;
    }
    printf("%d",result);
    return 0;
}
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.