Thread: How to terminate a loop if user inputs blank space in a string or just press enter

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    12

    Question How to terminate a loop if user inputs blank space in a string or just press enter

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    
    
    int main(void) {  
      int i;
      char *str;
      int len = 1; 
      str = malloc(sizeof(char*) * len);
      printf("\nEnter The String : ");
    
    
      while(scanf(" %[^\n]s", str) == 1){
        int upper_case = 0;
        int punctuation = 0;
    
    
        for(i = 0; i <= (int) strlen(str)-1;i++){
          if (isupper(str[i])){
            upper_case++;
          }
          if (ispunct(str[i])){
            punctuation++;
          }
            
        }
        if(*str == string.Empty){ 
            break;
        }
        
        printf("\nUppercase Letters : %d", upper_case);
        printf("\npunctuation : %d\n", punctuation);
        printf("------------------------\n\nEnter The String : ");
        
      }
      return (0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    The problem lies with your format string: " %[^\n]s". You need to remove the leading space, otherwise it would match whitespace until a character other than '\n' is encountered, hence it would keep waiting for input when you press enter.

    Related to this is your use of malloc:
    Code:
    char *str;
    int len = 1;
    str = malloc(sizeof(char*) * len);
    You're creating an array of 1 char, so it can only store the empty string, i.e., you have buffer overflow problems. I suggest that you go with a fixed size array that is reasonable large, e.g.,
    Code:
    char str[100];
    This way, you can write:
    Code:
    while (scanf("%99[^\n]", str) == 1) {
    Where the 99 indicates that a maximum of 99 characters can be read and stored in the array. I removed the trailing "s". You would need to insert a getchar() say at the start of the loop to get rid of the newline character from entering the line though.

    Also, it looks like you have something weird here:
    Code:
        if(*str == string.Empty){
            break;
        }
    You can just get rid of this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,799
    Code:
      int len = 1; 
      str = malloc(sizeof(char*) * len);
      printf("\nEnter The String : ");
     
     
      while(scanf(" %[^\n]s", str) == 1)
    Given that you've made 3 mistakes in as many lines, I'll suggest an alternative
    1. You use the wrong type in the sizeof - you want char, not char*
    2. You only allocate a single char - which is not enough to contain a useful string and it's trailing \0
    3. Your scanf blocks on newlines, but there is no other code to remove a newline from the input stream.

    Start with this
    Code:
    char buff[BUFSIZ];
    while ( fgets(buff,BUFSIZ,stdin) != NULL ) {
      // do stuff
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2015
    Posts
    12
    thakyou so much ...worked like a charm
    Code:
    while(scanf("%99[^\n]", str) == 1) {

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Press Enter to Exit a Loop?
    By lyelt in forum C Programming
    Replies: 7
    Last Post: 11-03-2014, 01:29 PM
  2. multiline string, endl, \n, blank line space
    By jackson6612 in forum C++ Programming
    Replies: 9
    Last Post: 04-20-2011, 08:50 AM
  3. Average of user inputs (in a loop)
    By SilentPirate007 in forum C Programming
    Replies: 13
    Last Post: 03-08-2010, 06:46 PM
  4. make user press enter / wierd code thingy
    By CorvusVita in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2006, 09:55 AM
  5. Getting input without the user having to press Enter...
    By Ranedhel in forum C++ Programming
    Replies: 18
    Last Post: 07-16-2003, 05:36 PM

Tags for this Thread