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 run !howto ask.
177 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 run !howto ask.
wait a second
i just have to set the first index of the array to 0??????
WUT
some1 explain
Read the second reply
Doing the thing mentioned in the first answer should be fine too (the remaining bytes will stay there unless overwritten)
ima try the first one
Ive never used memset before
when I tried using a for loop to set each element to 0 it did not work
;compile
#include<stdio.h>
#include<string.h>
int main(void){
char s[]="Cheese";
puts(s);
strcpy(s,"hi");
puts(s);
}
Cheese
hi
is this what you were talking about originally
well sorta
but will the length of s[]
be
6 or 2
cus idk if there is a bunch of stuff floating around in there from cheese
;compile
#include<stdio.h>
#include<string.h>
int main(void){
char s[]="Cheese";
printf("%s %zu\n",s,strlen(s));
strcpy(s,"hi");
printf("%s %zu\n",s,strlen(s));
}
Cheese 6
hi 2
what the heck
strcpy?
my code is an abomination of for loops and uglyness that gets a segmentation error every 5 minutes when I try to do something
and i could of just used strcpy
strcpy(a,b) copies the contents of b into a and the null terminator
so the array starts out as
C h e e s e \0
and after the call to strcpy
h i \0 e s e \0
yes
there shouldn't be any problem with that though
@soft valley Has your question been resolved? If so, run !solved :)
how would I use this
to just set the first element of it to \0
because if i try setting it
like s[0] =
'\0' i get warnings
and it does not work
cus my array is of type char
;compile
#include<stdio.h>
#include<string.h>
int main(void){
char s[]="Cheese";
printf("(%s)\n",s);
*s=0;
printf("(%s)\n",s);
}
(Cheese)
()
show the code
lemme make up like
something similar enough that entails what im trying to do but is not my code exactly
cus i don't think im allowed to share code here
for my homework
well
basically
what im trying to do
is harvest each char from the text file
if the char is indeed a letter
then store it into an array temp array
if its not
then i assume its like a space and then
i have a statement saying if the length of the main array is less then 30 and the length of the word + main array is less then 30 (just realized the first part is redundant)
strcat(array to store string, word)
then I want to reset the word array
and I tried *word = '0' and it did not work either
and well then after that set the index back to 0 for word
and get another ch if its not EOF
@winter grove does that make any sense at all? i don't blame you if it doesen't my explanation may of not been the best
are u still here?
yes
word has indeterminate value at the start, and assuming the first character read causes is_word_character to return 1, you will write to the first byte in word but then call strlen which reads up until a zero byte
so it has no way of knowing when to stop
hmmnm I see
you're trying to read the length of the string before it is fully formed and without adding a zero byte to end it
wait
why are you doing strlen(word) anyway can't you just use index
wait wdym
I dont call strlen if its ==1
you do strlen(word) without writing the zero byte to indicate the end of the string
char a[3];
*a='a';
// a is not a string yet, it still needs the zero byte
a[1]=0;
// now a is a string and strlen(a) will return one
ahhhhhhh
you should just be able to use the index that you are using to write the next byte as the length
so
before the if statement
Ima set word[index+1] = '\0'
then it should work
I think
you should be able to just replace both instances of strlen(word) with index
o
well I suppose
yeah ur right
but I wanna use strlen now for some reason
eh
if the code works the code works
i have a feeling that those word[index + 1] = ... are writing to the byte after what they should modify
if ab has been written then index is 2 so it writes to index 3 which is two after b
a b . n
^ ^-but it is writing here
|
never set to anything
wait what
huh
ohhh
wait
ahhh yeah
so not +1
oh no
there is still remanence of the previous words in word
its working better? but not really
the previous chars from words are overflowing into my array
I think I have to set every char in word to nothing
for this to work
what is RAWtext
progress is being made
its now no longer being weirdly merged
RAWtext is an array of just the text I extracted from a text file
a 2d array
its kinda bad naming sorry
cus im changing it
sorry
RAW text
is where im storing all this
lol
a 2d array
how big is the RAWText array since you're dumping all of the words into it
quite large
[101][101]
its working better now
its no longer random gibirish
the only issue I am encountering now
is its treating spaces infront of the text as chars
yeah its treating spaces as chars
it should be skipping the extra ones
it seems like the program will always add a space when it finds a non word character
hmmm ill have to fix that
right
thats what I was thinking
so ima say
if ch
is not a space
then add a space
change the first else here to an else if(index)
will that change anything?
so it won't try and add zero length words
o
that broke alot
lemme try to fix
i bet its a } thing
it was
holy heck
its working
I fixed the space thing too
with my if statement
NOOOOOOOOO
random garbage wtf
mmm i might have to fix another thing then
yeah its being weird with spaces
wait a second
might of patched it
i love how im giving a play by play
of something no-one is even witnissing
holy heck
it might actually be working
this is the greatest day of my entire C journey ever
@winter grove thank you very much for your insight
i have it working
!solved
Thank you and let us know if you have any more questions!
@soft valley
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. You can use !solved to close a post and mark it as solved.