Computing.Net > Forums > Programming > C Program: Finding End Of A String

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

C Program: Finding End Of A String

Reply to Message Icon

Original Message
Name: Dabbler
Date: August 1, 2002 at 20:37:43 Pacific
Subject: C Program: Finding End Of A String
Comment:

I need to figure out how to find the end of a string so I can put a null character '\0' at the end. For example I have this variable:

char phrase[100];

A person enters the phrase: "Hi Mom!"
Thats seven characters. I need to figure out how to put a null character as the eight character, or else I have 92 jibberish characters until it reaches the end of the 100 memory slots. Anybody have any suggestions?

Thanks


Report Offensive Message For Removal


Response Number 1
Name: Tom
Date: August 1, 2002 at 20:45:47 Pacific
Reply: (edit)

How exactly does that phrase get stored in the array? Do you use scanf or cin or something? In that case, it should automatically have the null terminator.


Report Offensive Follow Up For Removal

Response Number 2
Name: Jim
Date: August 1, 2002 at 20:48:10 Pacific
Reply: (edit)

Depends a lot on how you acquire the string. If you are doing scanf("%d", phrase); it will put the null terminator in there for you. If you're getting a character at a time until they hit the return key, and you have the length stored in a variable, it's pretty easy.

prase[length] = '\0';


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim
Date: August 1, 2002 at 20:49:42 Pacific
Reply: (edit)

doh. You wouldn't do scanf("%d", phrase); Maybe scanf("%s\n", prase);


Report Offensive Follow Up For Removal

Response Number 4
Name: Dabbler
Date: August 1, 2002 at 21:07:14 Pacific
Reply: (edit)

I planned on getchar(). scanf() only gets characters up to a space. So if somebody typed "Hi Mom!", scanf() would just capture "Hi". Explain how to get the length of the characters typed. Also I thought about saying:

char phrase[100];

while (i less_than phrase) {
phrase[i];
i++;
}

Or something to that extent, but that seems like a little much. Explain how to get the length of the characters typed.
Thanks


Report Offensive Follow Up For Removal

Response Number 5
Name: Tom
Date: August 1, 2002 at 21:34:54 Pacific
Reply: (edit)

Right, so you're using a loop. Well, like Jim said, you can use a variable (i) to count how many times you call getchar(). You could then simply:
phrase[i] = '\0';
i is incremented everytime the loop is executed, and getchar is called once everytime the loop is executed, so i should be the length of the string.
(i, as in the variable, i. I am not the length of the string) =P


Report Offensive Follow Up For Removal


Response Number 6
Name: hmm
Date: August 1, 2002 at 22:00:22 Pacific
Reply: (edit)

yeah, if you use getchar(), just increment a counter whenever it gets anything other then an enter.

but if you ask me, i would probably reset the array before i use it (cycle through from 0 to n, and put the null character in each position). from a performance standpoint, it might be bad, but in my classes, performance doesn't matter...


Report Offensive Follow Up For Removal

Response Number 7
Name: Dabbler
Date: August 1, 2002 at 22:42:00 Pacific
Reply: (edit)

I don't understand what hmmm said, but I do understand the looping and counting. Thats not a bad way to do it.


Report Offensive Follow Up For Removal

Response Number 8
Name: Jim
Date: August 1, 2002 at 23:46:12 Pacific
Reply: (edit)

Hmm was pointing out that if you fill the array with zeros before you start, there won't be any need to put one on the end. There will already be one there. As well as in every space after that, but that's kinda irrelevant.

There's probably a function to fill the array with zeros. Check your documentation for things like setmem() or memset().


Report Offensive Follow Up For Removal

Response Number 9
Name: cup
Date: August 2, 2002 at 07:39:04 Pacific
Reply: (edit)

As Jim says, set the array to 0

#define PHRASE_MAX 100
char phrase[PHRASE_MAX];
memset (phrase, '\0', PHRASE_MAX);

/* Get the input line */
gets (phrase);

Or if you prefer C++/STL

string phrase;
getline (cin, phrase);


Report Offensive Follow Up For Removal

Response Number 10
Name: Dabbler
Date: August 2, 2002 at 08:43:08 Pacific
Reply: (edit)

Neat way of doing that. Something I hadn't thought of. Does anybody know which way is faster and or more stable.


Report Offensive Follow Up For Removal

Response Number 11
Name: cup
Date: August 2, 2002 at 11:03:05 Pacific
Reply: (edit)

The C method is faster but it has one problem: if the string is more than 99 chars, it will crash.

The C++ method is more stable but, if you are using VC++, use you'll hit a bug where you have to input the first line twice. Unfortunately, to get around it, you're back to the C method. If you are not using VC++, it is not a problem.

char phrasec[PHRASE_MAX];
cin.getline (phrasec, PHRASE_MAX);
string phrase (phrasec);


Report Offensive Follow Up For Removal

Response Number 12
Name: Peter
Date: August 7, 2002 at 06:25:17 Pacific
Reply: (edit)

char *ptr;
char phrase[100];

ptr = phrase;

while((*ptr++ = getchar())!='\n') /*empty loop*/ ;

*--ptr = '\0';

Or something like that... haven't compiled it so please excuse syntax errors. As an engineer who spends most of his time optimising code for size/performance, the "fill the array with zeros" option brought me out in a cold sweat :) also... pointers are generally more efficient than indexing into an array.

Of course... an array limit check could also be included in the loop condition.


Report Offensive Follow Up For Removal






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you own an iPhone?

Yes
No, but soon
No


View Results

Poll Finishes In 7 Days.
Discuss in The Lounge
Poll History




Data Recovery Software