C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming
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.


Tutorials
| Forums

Download to Enter
| Contest Rules

DOWNLOAD INTEL® GPA FOR FREE

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 24th, 2003, 12:12 AM
SammyK's Avatar
SammyK SammyK is offline
Happy Monkey
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Location: UK (University of Kentucky)
Posts: 1,810 SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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:
Code:
  ifstream OpenFile("cpp-test.txt");
  static char ch[50];
  static char chin[50];
  static char user[50];

  while(!OpenFile.eof())
  {
    OpenFile.getline(ch,50,'\t');
    cout<<ch<<": ";
    OpenFile.getline(chin,50,'\n');
    cin>>user;
    if(user == chin)
    {
      cout<<"GOOD JOB! That is correct!\n\n";
    }
    else
    {
      cout<<"WRONG!   "<<chin<<"\n\n";
    }
  }
  OpenFile.close();
__________________

Reply With Quote
  #2  
Old September 24th, 2003, 12:48 AM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 610 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
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.

Frank Zappa

Reply With Quote
  #3  
Old September 24th, 2003, 01:05 AM
SammyK's Avatar
SammyK SammyK is offline
Happy Monkey
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Location: UK (University of Kentucky)
Posts: 1,810 SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level) 
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.

Reply With Quote
  #4  
Old September 24th, 2003, 01:35 AM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 610 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
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.

Reply With Quote
  #5  
Old September 24th, 2003, 08:40 AM
SammyK's Avatar
SammyK SammyK is offline
Happy Monkey
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Location: UK (University of Kentucky)
Posts: 1,810 SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 7 sec
Reputation Power: 45
Hey man thanks! This is some great information! As always I have a few more Q's for you.

I change my if to

if( !strcmp(user, chin) )

like you suggested. So let me get this stright. strcomp() returns false on match, but true on fail?

And as for the other functions:

isalpha() // Removes alph-numberioc characters?

isspace() // Removes spaces?

isdigit() // Removes numeric chars?

ispunct() // Removes punctuation?

I have tried using these functions and it gives me an error:

MakeFile.c:46: incompatible types in assignment of `int' to `char[50]'

Can these only be used with ints?

Sorry to keep asking questions. Is there a good site out there with a searchable database of C++ functions like PHP has @ www.php.net? Thanks again!

Reply With Quote
  #6  
Old September 24th, 2003, 08:45 AM
SammyK's Avatar
SammyK SammyK is offline
Happy Monkey
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Location: UK (University of Kentucky)
Posts: 1,810 SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 7 sec
Reputation Power: 45
Never minnd about the compile error I got. I was doing something stupid.

Is this invalid syntax?
Code:
  cout<<"isalpha(): "<<isalpha(test)<<endl;
  cout<<"isspace(): "<<isspace(test)<<endl;
  cout<<"isdigit(): "<<isdigit(test)<<endl;
  cout<<"ispunct(): "<<ispunct(test)<<endl;
When I run the proggie, and get down to that, it says "Segmentation fault"

Reply With Quote
  #7  
Old September 24th, 2003, 08:48 AM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed God (5000 - 5499 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 5,163 Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level)Onslaught User rank is Major General (70000 - 90000 Reputation Level) 
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";
}

Reply With Quote
  #8  
Old September 24th, 2003, 02:10 PM
SammyK's Avatar
SammyK SammyK is offline
Happy Monkey
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2001
Location: UK (University of Kentucky)
Posts: 1,810 SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level)SammyK User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 7 sec
Reputation Power: 45
Thanks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How do I "trim" a char array?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.

© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap