Hello i'm so new in the programming and want to learn some from you :) I'm doing a program in .c and i am stucked in a part. I want to get 3 or more inputs max size of 5 characters. (For example: HELLO, HI, GOOD, BYE) And i want to stack them in a new string which holds same letters only once from those 4 strings (Example: H,E,L,L,O,I,G,D,B,Y)
#include <stdio.h>
#include <string.h>
int main(void) {
char first[5], second[5], third[5], fourth[5];
printf("Enter 1st word: \n"); scanf(" %5s", &first);
printf("Enter 2nd word: \n"); scanf(" %5s", &second);
printf("Enter 3rd word: \n"); scanf(" %5s", &third);
printf("Enter 4th word: \n"); scanf(" %5s", &fourth);
char stack[21]; // i want a new string like this and then combine first 4 strings
// in this string...
return 0;
}
I hope you can let me know with which way i can do it. (I'm also new in the site. I searched for this but i couldn't find. Sorry if it exists.)
Try:
This should work for what you are wanting, this was a quick write, so there may be small errors!
Please update your code as following:
to
Please update to other lines.
First some comments on your code:
first
, …,fourth
arrays should be of length at least 6.stack
array should be able to accommodate 21 characters (again 1 for the terminating NUL byte). (Also mentioned by user3121023's comment.)scanf
, pass an argument of typechar *
, notchar (*)[6]
.first
already decays tochar *
so don't additionally take its address (as in&first
). Turn on your compiler's warnings (use at least-Wall
) to be informed about such bugs. (Also mentioned by Dere0405 while I was typing this answer.)scanf
is insecure. If the user inputs a string longer than 5 characters, you'll read beyond the end of your array. You could modify the format specifier to read%5s
to tellscanf
to stop reading after the 5th character. However, this will leave excess characters at the end of the line. A better option would be to usefgets
orgetline
to read an entire line of input. Alternatively, simply pass the strings as command line arguments (my preferred solution).Now to the actual problem:
I won't give you a complete solution but only some hints because this looks very much like homework. (Unfortunately, someone else has already given you the full code so you will likely ignore my answer.)
You'll have to loop over all five strings and check for each character if it was already added to the
stack
. If so, continue, otherwise, append it to thestack
. To loop over a string, we can use the following idiom.Alternatively, if we don't need to refer to the current index, the following idiom is more compact.
Note how we use the fact that
first
– being a C string – is terminated with a NUL byte which evaluates to false. Otherwise, we would not know where to stop iterating.Now that we know how to loop over the characters of a string, how can we check whether a character was already added? Two solutions come into mind:
Loop over
stack
and compare each element to the current character in question. While for your short strings, this might be the method of choice, it will grow inefficient for longer strings.Create a counter for each character and increment it whenever added to
stack
. You can use the fact thatchar
s are just numbers. So you could create an array with 256 elements (there are 256 differentchar
s) all initially set to 0 and then increment the position for the character currently added. For example:And then later in your code:
The
if (counters[(unsigned char) (*pos)]++)
is a little tricky. First,*pos
dreferences the pointerpos
to yield the current character which is then interpreted as anunsigned char
because arrays cannot have negative indices. That position is then looked up in thecounters
array and evaluated in theif
statement. Finally, the value is incremented (but only after comparing) via the post-increment operator.Don't forget to terminate
stack
with a NUL byte at the end.