Coding Global Background
Coding Global

help with break statement;

Archiviert 6 months ago
2
4 Nachrichten
2 Mitglieder
Erstellt a year ago
Aktualisiert a year ago
In Discord öffnen
B
bestlypvp
Verified
program to input a string and a letter. prints the first index occurance of the letter in the string.

break statement isn't working.
everything else seems to work just fine



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

int main() {

char input_string[50];
fgets(input_string,sizeof(input_string),stdin);

int lenth=strlen(input_string);
if (lenth>0 && input_string[lenth-1]=='\n'){
    input_string[lenth-1]='\0';
}

char letter;
scanf("%s",letter);

int flag=0,spaces=0;
int index=-1;
for (int i=0;i<lenth;i++){
    if (input_string[i]==' '){
        spaces++; 
        continue;
                    }
    else if(input_string[i]==letter){
        flag=1;
        index=i;
        break;}
    
}

printf("%d",spaces);
printf(" %d",index);

if (flag==0){
    printf("-1");}
else{
    printf(" %d",index-spaces);
}

    return 0;
}





basically i realise that the break command isn't working but no matter what i do or ask in chatgpt it isnt explaining whats wrong.
output example down below:
help with break statement;

Antworten (4)