#How do I set the contents of a string to null so that strlen() does not count it?

177 messages · Page 1 of 1 (latest)

crimson sandalBOT
#

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.

cedar oar
soft valley
#

wait a second

#
  1. thank you
#

i just have to set the first index of the array to 0??????

#

WUT

#

some1 explain

cedar oar
#

Read the second reply

soft valley
#

I have to use memset?

#

hmmm

cedar oar
#

Doing the thing mentioned in the first answer should be fine too (the remaining bytes will stay there unless overwritten)

soft valley
#

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

winter grove
#

;compile

#include<stdio.h>
#include<string.h>
int main(void){
    char s[]="Cheese";
    puts(s);
    strcpy(s,"hi");
    puts(s);
}
coarse dockBOT
#
Program Output
Cheese
hi
winter grove
#

is this what you were talking about originally

soft valley
#

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

winter grove
#

;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));
}
coarse dockBOT
#
Program Output
Cheese 6
hi 2
soft valley
#

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

winter grove
#

strcpy(a,b) copies the contents of b into a and the null terminator

winter grove
#

yes

#

there shouldn't be any problem with that though

soft valley
#

Hold up

#

I must try to implement this

#

this is apart of #1040360318351712437

crimson sandalBOT
#

@soft valley Has your question been resolved? If so, run !solved :)

soft valley
#

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

winter grove
#

;compile

#include<stdio.h>
#include<string.h>
int main(void){
    char s[]="Cheese";
    printf("(%s)\n",s);
    *s=0;
    printf("(%s)\n",s);
}
coarse dockBOT
#
Program Output
(Cheese)
()
winter grove
soft valley
#

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?

winter grove
#

yes

soft valley
#

sorry for your eyes

#

after reading my abomination

winter grove
#

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

soft valley
#

hmmnm I see

winter grove
#

you're trying to read the length of the string before it is fully formed and without adding a zero byte to end it

soft valley
#

wait

winter grove
#

why are you doing strlen(word) anyway can't you just use index

soft valley
#

index?

#

oh

#

ohh

#

lol

#

bruh

#

that would solve everything

#

bruh

soft valley
#

I dont call strlen if its ==1

winter grove
#
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
soft valley
#

ahhhhhhh

winter grove
#

you should just be able to use the index that you are using to write the next byte as the length

soft valley
#

so

#

before the if statement

#

Ima set word[index+1] = '\0'

#

then it should work

#

I think

winter grove
#

you should be able to just replace both instances of strlen(word) with index

soft valley
#

o

#

well I suppose

#

yeah ur right

#

but I wanna use strlen now for some reason

#

eh

#

if the code works the code works

winter grove
#

i have a feeling that those word[index + 1] = ... are writing to the byte after what they should modify

soft valley
#

im trying to put a space after the word

#

so its not all

#

word1word2

winter grove
#

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
soft valley
#

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

winter grove
#

what is RAWtext

soft valley
#

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

winter grove
#

how big is the RAWText array since you're dumping all of the words into it

soft valley
#

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

winter grove
#

it seems like the program will always add a space when it finds a non word character

soft valley
#

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

winter grove
#

change the first else here to an else if(index)

soft valley
#

will that change anything?

winter grove
#

so it won't try and add zero length words

soft valley
#

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

crimson sandalBOT
#

Thank you and let us know if you have any more questions!

crimson sandalBOT
#

@soft valley

Please Do Not Delete Posts!

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.

soft valley
#

oh

#

ok

#

!solved