This question already has answers here:
C dynamically growing array
(10 answers)
Closed 9 years ago.
How do I read in a c-string in to a character array without knowing the size of the string that the user will enter ?
Without any code or further description of your issue it's hard to know what you're trying to achieve, but, one of the following might be appropriate for your needs:
Use a preallocated array of some maximum size you know is greater than the number of characters that will be entered.
Create an empty std::string, and then use the string "+=" operator for each character entered. Then you can convert back to an array using the c_str() method.
Related
This question already has an answer here:
Python-like array filling - C equivalent
(1 answer)
Closed 2 years ago.
I'm looking for an "append" (Python) or "push_back" (C++) equivalent in C to fill an array of strings (char). Does it exists?
The question doesn't make sense because you cannot add elements to an array in C. In C, you set the size of the array when you create it, and the size cannot change.
If you want to "append", you have to create a big array (e.g. 1000 items), and use a separate variable to remember how many items you're actually using (the rest are spare).
This question already has answers here:
How do you allow spaces to be entered using scanf?
(11 answers)
Closed 3 years ago.
I need to read the text using scanf, but what I know scanf is reading characters to the first space. I don't know that it's possible to read more words into the array by scanf and i have no any idea.
That's not correct; scanf reads according to the format string you give it.
Nothing keeps you from using a format that reads past spaces, returns, or whatever else, for example scanf("%50c",buffer); will read 50 characters, no matter what they are; or scanf("%[^|]", buffer); will read everything up the first |. Read up on the definition of the scanf family.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to C and would appreciate your help in a code.
I need to allocate dynamically an array and check which prefix in the string is the shortest that it's concatenation builds the string, and print it and it's length.
Here are a few examples for the outputs:
for "ababab", the output should be: "ab" of length 2
for "aaaa", the output should be: "a" of length 1
for "abcaabca", the output should be: "abca" of length 4
for "abcdefg", the output should be: "abcdefg" of length 7
for "acacaac" the output should be "acacaac" of length 7
My problem is that I don't know how to build the function that supposed to to it. The cases in which the string contains only the same letter, or all letters are different from each other are fine, but I don't know how to take care of all the other cases.
I can't use another string for this code, but I'm allowed to use other pointers
to help me.
Thanks
I will not write the effective code, but the idea itself.
First, this "prefix" of yours has a length that is a divisor of the big length. So if the length i of the prefix doesn't divide the length n of the string then it should be skipped.
To check if a length i is valid, then you need to compare the characters from positions j and j+i, for all possible values of j.
It can be pretty simple, two nested loops, the outer one being a for, from 1 to length/2, and the inner a while, using a pointer increased in each step by the value of the iteration variable of the outer loop. Inside the loop you can use strncmp() to compare the a proper substring, and break the loop if comparison fails. If all comparisons are OK, you have found the "shortest prefix", so you can break the (for) loop. The for loop can be optimized too, by skipping the lengths that are not divisors of the input string length.
This doesn't require making a copy or copies.
And yes, I will leave you actually write the code.
You said you cannot use another string, hopefully You are allowed to use a small temporary array of booleans to count the number of different characters in the string.
1- Find the number of different characters in the string, using an array of boolean. Let this number be x.
2- check if the first x characters constitute a prefix. (check each pointer position p against p+x, p+2x, etc). If unsuccessful, the shortest prefix is the whole string.
In between, you can make it faster by quickly checking if x divides the length of the string..
This question already has answers here:
Reading a text file backwards in C
(5 answers)
Closed 7 years ago.
I am reading a one line string from a file and I need to read it into an array in reverse order. How can I accomplish this?(I am using C).
So, my file looks like:
ACGTGCGATCGATCGATCGATATCGATCGTCTGCTTAAGCTC
And I want my array of chars that I read into to look like:
CTCGAA...
Thanks in advance.
Read the file front-to-back into the array, then reverse the contents of the array in place. That's the best way to do that as reading a file backwards is a very slow operation.
This question already has answers here:
Separate the alphabet and digit such that their relative order remains the same in O(n) time and O(1) space
(2 answers)
Stable separation for two classes of elements in an array
(3 answers)
Closed 9 years ago.
Given an array that contains alphabets and numbers, provide an algorithm to move the numbers to the front of the array and alphabets to the end of the array without changing their order in the given array.
Expected Space complexity: In place
Expected Time complexity: O(n)
E.g:
Input:
{1,2,a,3,b,c,4,d,5,e}
Output:
{1,2,3,4,5,a,b,c,d,e}
I came across this question in a website and couldn't figure out an algorithm that satisfies the space and time requirements. Can anyone please tell if it is possible to solve it inplace in O(n) time with the algorithm?