# Problem with a char

This topic is 4780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a char that looks like the following: MARBLE.BMP Now, I want to base another char on the above char, but skipping the .BMP part of it (i.e. this new char should look like this: MARBLE). What should I do? I have tried a loop going through the above char, letter by letter, but that only crashed the entire thing (no compilation errors though).

##### Share on other sites
you should give us some more information, BUT if your using C++ or C or anything similar, this should work...

by the way, a char is one Letter, what you talk about is a string!
(a string of chars)

say you have your word 'MARBLE.BMP' in char array, or as a char pointer:

char string[32];
or
char *string = new char[32];

//fill with string,

you could easly remove everything behind a dot like this:

//find out if we have a dot!
bool do_we_have_a_dot = false;
for (int i=0;i<strlen(string);i++){
if(string[i] == '.') do_we_have_a_dot = true;
};
if (do_we_have_a_dot){

int pos = strlen(string) - 1;

//remove everything behind the dot, terminate with a 0
while (string[pos] != '.'){
string[pos] = 0; //you could drop this line, no difference
pos -= 1;
};

//remove the dot, and terminate the string with a null
string[pos] = 0;

};

Done!

You should explain your problem better, or at least give us some code. I know about 1000 ways to make a string algorithem blow up my code!

##### Share on other sites
#include <string.h>// If you want to modify the string cutting out the .BMP partchar* firstDot = strchr(myString, '.');if(firstDot)   *firstDot = NULL;

#include <string.h>// If you want to build a new stringchar newString[256];char* firstDot = strchr(myString, '.');if(firstDot){   int firstDotPos = firstDot - myString;   strncpy(newString, myString, firstDotPos);   newString[firstDotPos] = NULL;}

##### Share on other sites
1) A 'char' is just a single character. Actually, it's not even that; it's a variable which stores a single-byte value, either signed or unsigned (but not specified which, and treated as a separate type from either), with the implicit assumption that the possible values correspond to characters though the miracle of ASCII.
2) char * is really a pointer to a single char. It gets treated as a pointer to a sequence of char where the last is expected to have a zero value, but doing this sort of thing to represent text data is an ugly 80s hack.
3) Confusing the character zero value '\0' (ASCII NUL) with the pointer zero value (NULL) removes documentation value and is not a particularly good idea. Good style dictates not using "NULL" at all in C++.

Assuming you have the option of using C++, you should be working with std::string.

#include <string> // no .h!string original = "MARBLE.BMP"; // constructing/assigning from a char * works// But I suppose you will generally be reading names in from file...// To make a new string:string withoutExtension(original, 0, original.find('.'));// constructed from the original using just that range.// To overwrite the old string:original.assign(original, 0, original.find('.'));// Or, possibly more efficiently:original.resize(original.find('.'));// In all cases this will remove everything including and after the FIRST dot.// To remove just the LAST extension (if that makes a difference), use 'rfind' instead of 'find'.// I didn't actually know exactly how to do this until just now;// I just asked [google] for 'std::string', and looked up the// methods in SGI's doc.

##### Share on other sites
Quote:
 Original post by ZahlmanGood style dictates not using "NULL" at all in C++.
I'm sorry but I completely disagree on this one point.
NULL should be used always, for and only for, pointers. Just as you would always use nil, for and only for, pointers in Pascal/Delphi... etc.
i.e. Please don't use 0 for two distinct uses. NULL exists for a reason.
Thankyou.

##### Share on other sites
We had an argument over the NULL vs 0 issue... basically it's a matter of personal preference. Even Stroustup suggests the use of 0, but I feel that NULL makes the code much more readable. Who cares if it's not officially a part of the C++ standard?

My two cents :)

Ryan

##### Share on other sites
Quote:
 Original post by Ryan Clarkover the NULL vs 0 issue... Ryan

it was not NULL vs 0, it was NULL vs '\0'.
NULL shouldnt be interchanged with the string-terminator.

##### Share on other sites
The creator of C++ recommends using '0' rather then the 'NULL' macro, using macros is bad style, no type checks can be performed.

##### Share on other sites
Yeah, yeah, I was just typing the code on the spot. I guess I normally use '\0' or 0 for string termination. It's supposed to be a char after all. I'm not an uber-standard kind of guy though and I wouldn't be too concerned with NULL being defined as anything but 0. Most code would blow up if it wasn't anyways.

##### Share on other sites
Ok, thanks! Will try to make it work :)

##### Share on other sites

This topic is 4780 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.