Thread: Working with strings [using scanf()]

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    5

    Working with strings [using scanf()]

    I have sadly never been given a proper lesson in regards to strings when it comes to the C language, so I decided to hunt some threads myself, with little success. I am hoping I can be set to walk the right path with this thread

    My Task:
    Take a string as a user input using scanf. Compare braces and output "Missing Braces!!" if the open and close braces don't match in numbers.

    I'd appreciate it if it could be kept on basic functions and simple terms, I just need to read a string, probably loop through it and take note of open and close braces. I'm simply having trouble working with strings. Following is the code I've built so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int userInput[20];
    const int bOpen = 123;  // {
    const int bClose = 125; // }
    
    void takeInput(void) {
    	printf ("Please enter your string:\n");
    	scanf ("%s",userInput);
    }
    
    int checkBraces(void) {
    	int i, count=0;
    	for (i=0;i<20;i++) {
    		if (userInput[i] == bOpen) count ++;
    		else if (userInput[i] == bClose) count--;
    	}
    	return count;
    }
    
    void main (void) {
    	takeInput();
    	if (checkBraces() != 0)
    		printf ("\n\tMissing Braces!!\n");
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    userInput should be a char array based on your wanting to use it for an argument in scanf as a %s. (did you not get a compile warning about this?)

    Overall, why use ints at all, since the native type for what you are entering is a char.?

    How about:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char  userInput[20];
    const char bOpen = '{';
    const char bClose = '{';
    
    void takeInput(void) {
    	printf ("Please enter your string:\n");
    	scanf ("%s",userInput);
    }
    
    int checkBraces(void) {
    	int i, count=0;
    	for (i=0;i<20;i++) {
    		if (userInput[i] == bOpen) count ++;
    		else if (userInput[i] == bClose) count--;
    	}
    	return count;
    }
    
    int main (void) {
    	takeInput();
    	if (checkBraces() != 0)
    		printf ("\n\tMissing Braces!!\n");
    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    5
    Thank you for the reply, in regards to avoiding chars, is due to a phobia with comparison operators, I always seem to do it wrong :P One final problem is the fact that my program cuts off at whitespaces, I need it to take a full string in until I hit enter.

    edit: doing a basic change it turned out to work without any issues:

    Code:
    scanf ("%[^\n]",userInput);
    Last edited by Luponius; 04-26-2011 at 08:47 AM.

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    This construct

    Code:
    %[^\n]
    is why I come to these boards. Just learned it the other day.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Luponius View Post
    edit: doing a basic change it turned out to work without any issues:
    you still have an issue of memory overrun in case when user enters string longer than 19 chars - add width modifier to your format to prevent it.
    Code:
    scanf ("%19[^\n]",userInput);
    also - would be a good idea - start using local vars instead of global ones. and pass them as parameters to your functions
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf for strings?
    By pobri19 in forum C Programming
    Replies: 7
    Last Post: 05-31-2008, 03:47 AM
  2. why scanf() dont working?
    By RAJJUMOTE in forum C Programming
    Replies: 10
    Last Post: 12-16-2007, 10:29 PM
  3. Every other scanf not working
    By Rob20050 in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 12:48 AM
  4. char pointer working in scanf but not in cin.
    By sawer in forum C++ Programming
    Replies: 14
    Last Post: 06-15-2006, 02:15 AM
  5. Why isn't this working? using scanf and stuff
    By jumboman in forum C Programming
    Replies: 2
    Last Post: 07-17-2003, 01:56 PM