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