guiromero
guiromero

Reputation: 77

Output not showing in C

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?

Upvotes: 1

Views: 859

Answers (2)

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

Upvotes: 1

Zachary Vander Klippe
Zachary Vander Klippe

Reputation: 108

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!

Upvotes: 1

Related Questions