How to use scanf without determined amount of input - c

I've been searching a while for this, and couldn't find an answer. I will be appreciated if someone knows how do to that!
PROBLEM: I must code a program that will store some numbers, but I don't know how much numbers there will be! What can I do?
I was wondering if I could use timing to get things done. I mean, if 5 secs has passed and there is no input of data, then start processing these numbers. It would work, but I couldn't code this. Can someone help, please?

1) first solution:
You can ask the user to enter the number of desired element at the beginning.
2) second solution:
Keep scan numbers till you get EOF from the user. and store the input number into a linked list or a dynamically allocated array (resize your array size withe the realloc)
3) third solution
keep scan numbers with a timeout. If there is no input during the timeout then the program will consider that the user have finished input numbers and then the program stop reading from stdin. The input numbers could stored into linked list or dynamic array as indicated in the second solution. Use select() with the scanf() in order to add the timeout behaviour as indicated in this answer

Related

Best Search-Algorithm to search an Input String

I have a theoretical question.
I'm trying to find the best search algorithm to deal with the following problem: I want to process a input String, the input String can have up to 7 independent and valid arguments (parameters), which start different segments in my code. All parameters can appear at any position inside the input, which means the input does not have a order.
My first idea was to look at the input string and do a strcmp for all individual valid inputs at every position inside the string, which would mean that I would basically search the string until I find a match or reach the end of the input. However, this would have very bad runtime latency as I would iterate the input with n!.
I'm wondering if there is a better way to search the input, maybe something where I can decrease the sample size after a valid input has been found, so I dont have to look at this position again. I would be glad if someone could help me to find a search-algorithm with a better runtime efficiency.
As far as I know, its not possible to decrease a stack size. I guess the only way to improve the efficiency is to skip positions inside the stack, based on a bool value? Every position that I can skip drastically improves the runtime, as it would have to be compared with all 7 inputs.
Thanks for reading :)
I'd bet that your libc implementation does this quite well ;)
strstr() is the function you are looking for, go check this man : http://manpagesfr.free.fr/man/man3/strstr.3.html

Program crashes when using dynamic memory allocation in array

I am given a task which is as follows:
User enters a string of symbols which can be only < (less), > (greater) or = (equal) and string ends with semicolon. The task is to generate numbers between those symbols while the very first number is 1. An example: if user enters <>>>=<; the program should generate numbers according to those symbols, like this: 1<2>1>0>-1=-1<0. In my program everything is working...sort of. Sometimes if I enter 15 symbols, everything works just fine, but when I enter 20 symbols, my program crashes so my guess is I have issues with memory allocation, but not sure where... Any help would be appreciated! Here is my code:
Problem solved!
sizeof(simboliai) returns size of the pointer (usually 4) not the actual length of allocated array. You need to keep track of that separately. For example, in another variable.

String comparison in C programming

How to read and compare strings from dynamic output console in C Programming?
Consider, you will get one output at a time. You need to consider the output from a Program as an input to other program function. The example output of the Program 1 as follows,
A123#345H5KGH3***STATE***GETRUNOMMUNICATION5619***CONNECTED***HIEDSAKNFH***OK***
I need to get the statement STATE:CONNECTEDOK if this statement present in the output console, then the next program(function) will start.
Note:
I am getting one output at a time
Consider the first output as "A" the next one will be "A1" and the following output as "A12".
I thought to use array to store values, but the output range( unlimited) makes it very lengthy and difficult to configure before hand ,since the length of the array is unknown.
If the specified statement appears in the output console, then it should prompt some message.
No spaces in between the two continues outputs.
I thought to use strings, so my question are as follows,
How to read the string from output console, when the length of the string is unknown?
What function I need to use to perform the comparison ( I already know the statement "STATE:CONNECTEDOK") with unknown length of output?
How to store the output data, when the range or length is unknown?
Thank you.
Assuming you don't mean that you want to identify the data graphically from the actual console window, but instead that you can pipe the data to your program, I think the best bet is to use a state machine, namely an "acceptor".
You simply read a character at a time, and feed it into the state machine.

Java Scanner Advise

This is just a question, please do not give me the answer. I need direction:
Assume stdin is an object reference to a Scanner, and count is an int that has been initialized to 0:
Read integers from stdin counting how many integers you see in the range 0-50 inclusive.
Stop when you read an integer outside the range.
count should be updated to indicate how many integers you read before you encounter an integer outside the range.
You can use a while loop to control the range, and then just keep reading in and adding to the count. It would look something like this:
while nextInt() is in range:
add 1 to count
It's really pretty simple, but takes some time to understand why. Basically, you have count and stdin. In the while loop condition, you check the next number from stding to see if it is in the proper range, and then add to count if it is.

Taking values through input and diplaying them during run time

I need to write a program where during run time, a set of integers of arbitrary size will taken as input. They will be seperated by white space. At the end, a new line is given, showing the end of input. How do I save them into an array of integers so that i can display them later. I think it is a little difficult because the number of values that will be entered is not known during compilation
Sounds like homework.
Correct me if I am wrong and I will give you more than hints.
You can either declare an array of a really large size that would not possibly be filled by the user input, then use scanf or something like that to grab the integers until you hit '\n', or you can grab each integer at a time, allocating memory as you go, using a combination of malloc and memcpy calls. The first option should never be done in a real world problem, and I am certainly not advocating such practices even though your textbook probably tells you to do it this way.
There is an example just like this in K&R.
This is a typical problem you will have in C. The solution is usually one of two options.
Use a really large array that is large enough to hold the input. Sometimes this is a poor option when the data could be really large. An example of when it would be a bad idea is when you are saving a video frame or a large text file to the array. This also opens you up to a buffer overrun attack in older versions of Windows. However, this is sometimes a good quick hack solution for smaller (homework) programs where you can count on the user (i.e. your professor who is not trying to break your program) to not input 1000's of characters. Usually this is considered bad practice, please consider my 2nd option for the security reason I mentioned before.
Use dynamic arrays (i.e. malloc). This is probably what your professor wants you to do as this sounds like a typical problem to use when a student is first learning pointers and arrays. This is a great approach, just remember to call free on your memory when you are finished. The tricky part here is that you still have to know the size of the array you want ahead of time (not at compile time though of course).

Resources