When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
48 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
I would love some advice on how to get this to work. Is it possible to remove an element from an array?
are you trying to make the program efficient or are you trying to get it done quickly
if you're going for efficiency you shouldn't use strcmp, you should use a hash function
I'm going to keep strcmp but i want to know if it's possible to remove an element from an array?
yes it is
And what function or method does that?
you should create the function yourself. what you're going to need to do is assign the ith elment of the array to the value of the last element of the array, then use realloc() to reallocate the array to one element of size smaller
realloc() is not something we have gone through on the course. Is there any other method?
strcmp in this case won't work, as you are trying to compare characters and not strings, you have to just use a normal comparison
yes
I had gpt help me
// 2023 Alexander Johansson
#include <stdio.h>
#include <string.h>
int main()
{
char array[60];
while (scanf("%59s", array) == 1)
{
int len = strlen(array);
for (int i = 0; i < len - 1; i++)
{
if (array[i] == array[i + 1])
{
for (int j = i + 1; j < len; j++)
{
array[j] = array[j + 1];
}
len--;
i--;
}
}
printf("%s\n", array);
}
}
I'm just a little confused on what the last for loop does
for (int j = i + 1; j < len; j++)
{
array[j] = array[j + 1];
}
that should work
the last for loop goes through the array from the current index setting every element to the elemtn in front of it
you can visualize removing an element from an array as looping over the elements to it's right and copying them by 1 to the left
[51] [53] [12] [74] [10]
XX
[51] [12] [74] [10] [10]
you just have to implement such thing by yourself, but we can of course help if you feel stuck
(of course you'll need to handle the last element being there twice, but you can decide for yourself how to do such thing)
So if the word that is put in is e.g. Programming
then programing
yes
yeah
But what happens with the final 2 dublicate g:s?
Programming
Yes if i have this, at the end i will have dublicates g:s
no you won't
why would you have duplicate g's
the g is assigned to the null terminator
after the n is assigned to the g
you can decide yourself what to do in that situation, but because you are dealing with a string you probably want to set the last elemento to 0, the string terminator
Ah yeah i see
!solved
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity
you know that's funny cause it looks like not solved