Discuss How do I "trim" a char array? in the C Programming forum on Dev Shed. How do I "trim" a char array? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
The ASP Free website provides in-depth information on the latest developer tools available from Microsoft. Our cadre of writers, highly experienced industry experts, reveals the best ways to use established technologies as well as new and emerging technologies. Our coverage of Microsoft's development and administration technologies is among the most respected in the IT industry today.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Intel® Graphics Performance Analyzers is a powerful tool suite for analyzing and optimizing your games, media, and graphics-intensive applications. Used by some of the best developers on the planet, Intel GPA lets you maximize your app’s performance.
Posts: 1,810
Time spent in forums: 4 Days 7 sec
Reputation Power: 45
How do I "trim" a char array?
Is there a library that contains a function to trim a char array? In PHP the trim() function removes any white space or newline chars, or carriage returns... etc. from the beginning and end of a string.
The reason is I have a file set up like this:
Code:
name_one Sam
name_two Fred
name_three Bob
I am opening the file and cout-ing name_one and asking the user for input. Then I compare what they typed in to Sam. But it doesn't ever match, so I am assuming that it is the \n character that is being past from the user, and that's why I want to trim it. Here is a snippet of my code:
Posts: 610
Time spent in forums: 3 Days 14 m 19 sec
Reputation Power: 25
>>if(user == chin)
What is contained in the strings here?
test they contain what you think.
try a
if(strstr(user,chin)!=NULL)
to see if both are similar then print them out.
or add the '\n'
something like
chin[lstrlen(chin)]='\n';
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.
Posts: 1,810
Time spent in forums: 4 Days 7 sec
Reputation Power: 45
Thanks man!
if(strstr(user,chin)!=NULL)
That worked like a friggen charm! So now, does strstr() just compare two strings by ignoring the new line char? I am coming to C++ from PHP, and PHP can compare two strings with a simple ==.
I also tried chin[lstrlen(chin)]='\n'; to see if that worked also, but it started complaing about not including the correct library. Which one do I need for lstrlen()? Thanks again for your help.
Posts: 610
Time spent in forums: 3 Days 14 m 19 sec
Reputation Power: 25
sorry, my bad.
In C++ you can use the == for strings but not in C.
I prefer to use the string functions (as I learnt C first)
My understanding is that it == will compare ALL the eements of the array including teminators ect causing your problem.
to start with
strstr() only finds occurences of one string in another. I expected you to just use it as a test....
ie turn is in return and would match.
the return is apointer to the first occurence or null if no match.
strcmp() will return 0 if both exactly equal. It also has version that ignore case ect.
if(strcmp(user,chin)==0)//match
all these come with a windows (WIN32) version preceeded with an l ie lstrcmp()
so use
strlen() not lstrlen()
all are from string.h
isalpha(), isspace() isdigit() ispunct() will help in triming the strings if needed.
Posts: 5,163
Time spent in forums: 6 Days 1 h 34 m 20 sec
Reputation Power: 790
strcmp() returns an integer value representing which argument is greater than the other. A value of 0 (zero) means that they are equal, and positive value means that the left is greater and a negative means that the right is greater.
The other functions are meant to be tested on a single character and return 0 or 1. They are tests and do not alter the character passed to it.
isalpha() tests to see if the character is from A to Z
etc...
you use it like:
Code:
char test='a';
if (isalpha(test)) {
cout << test << " is alpha\n";
}