0
#include <stdio.h> #include <string.h> int main(){ char c[20], result[50]; int bool = 0, count = 0, i; while(fgets(c,20,stdin) != NULL){ int stringSize = strlen(c); if(stringSize == 11){ int ascii = (int)(c[i]); for(i = 0; i < stringSize; i++){ if(ascii >= 'A' && ascii <= 'Z'){ bool = 1; } } } } if(bool == 1){ count++; strcat(result,c); } printf("%d", count); printf("%s",result); } 

Good morning, I am fairly new to programming, and I've spent quite a while Googling and searching around for this issue already, but I can't seem to wrap my head about it. Basically I'm trying to filter an fgets so that it reads each string, and if they're capital letters, they're "valid". However, I can't even get the fgets to stop accepting more input.

Edit: The idea is to store in result every String that has 10 capital letters, and for the fgets while loop to break once the user gives no input ('\0')

11
  • Have you tried entering Ctrl-D (Linux) or Ctrl-Z (Windows)? Or is the idea to accept one input string when it complies? Commented Nov 22, 2021 at 9:05
  • 1
    int ascii = (int)(c[i]); uses i uninitialized. It looks like this statement should be in the loop, not before. Commented Nov 22, 2021 at 9:05
  • If the idea is to accept just one string if it is all uppercase, you need to reset the flag at the start of each fgets loop (not outside the loop). Then if any character isn't upper case, set the flag. But the problem statement isn't clear - please show an example input and required output. Commented Nov 22, 2021 at 9:09
  • I've edited with the intentions, my bad, should have been more clear. I basically want it to run for as long as there is an input, and for result to only store strings with 10 capital letters. Commented Nov 22, 2021 at 9:13
  • 1
    Not related to the problem, but this is a bad idea: int bool = 0. There is a type bool in stdbool.h As soon as you need this for other purposes, your code will break. You should not use identifiers that are defined by C standard even if you don't yet include the related headers. Commented Nov 22, 2021 at 9:41

1 Answer 1

2

If you are entering strings from the standard input stream then it is better to rewrite the condition of the while loop the following way

while( fgets(c,20,stdin) != NULL && c[0] != '\n' ){ 

In this case if the user just pressed the Enter key without entering a string then the loop stops its iterations.

Pay attention to that fgets can append the new line character '\n' to the entered string. You should remove it like

c[ strcspn( c, "\n" ) ] = '\0'; 

Then you could write

size_t n = strlen( c ); if ( n == 10 ) { size_t i = 0; while ( i != n && 'A' <= c[i] && c[i] <= 'Z' ) ++i; bool = i == 10; } 

Pay attention to that it is a bad idea to use the name bool because such a name is introduced as a macro in the header <stdbool.h>.

Also it seems this if statement

 if(bool == 1){ count++; strcat(result,c); } 

must be within the while loop. And the array result must be initially initialized

char c[20], result[50] = { '\0' }; 
Sign up to request clarification or add additional context in comments.

1 Comment

Nice... I'd assume char* nl = strchr(...); if(nl) { *nl = 0; } to be more efficient, though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.