need some help with C string

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 283
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 1
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: need some help with C string

 
0
  #31
Jun 29th, 2009
so i am just going to have them read in a string for the input of the sentence and a c-string for the char they are trying to find.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #32
Jun 29th, 2009
I'd recommend starting with a basic C book.
Did the teacher say no pointers?
  1. char MyBuf[ 256 ];
  2. const char *p;
  3. strcpy( MyBuf, "Happy Fourth of July";
  4.  
  5.  
  6. p = a.Find( MyBuf, 'u' ))
  7.  
  8.  
  9. const char * Search::Find( const char *pStr, char c )
  10. {
  11. while ( *pStr ) // Loop until terminator found
  12. {
  13. if ( c == *pStr ) // Match found?
  14. {
  15. return pStr; // Return the pointer
  16. }
  17. pStr++;
  18. }
  19.  
  20. return (const char *)NULL; // Not found return NULL
  21. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 283
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 1
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: need some help with C string

 
0
  #33
Jun 29th, 2009
most recent code

//main
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. #include "strextra.h"
  5.  
  6. int main()
  7. {
  8. Search a;
  9. cout<<"enter max number of characters in the sentence";
  10. int number;
  11. cin>>number;
  12. cout<<"enter a sentence";
  13. string mycstring;
  14. cin>>mycstring;
  15. cout<<"my string is\n"<<mycstring<<"\n";
  16. cout<<"enter the character you are trying to find in the string above\n";
  17. char alpha;
  18. char lett[2];
  19. cin>>alpha;
  20. lett[0]=alpha;
  21. lett[1]='\0';
  22. cout<<lett[0]<<lett[1];
  23. a.find(mycstring, lett[0]);
  24. }
//implementation
[/code]
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#include "strextra.h"

int search::find(string mystring,char letter[0])
{
return strchr(mystring,letter);
}
[/code]
//interface
  1. #ifndef STREXTRA_H_INCLUDED
  2. #define STREXTRA_H_INCLUDED
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Search
  8. {
  9. public:
  10. int find(string mystring,char letter[0]);
  11. int find(char mystring,string word);
  12.  
  13. };
  14.  
  15. #endif // STREXTRA_H_INCLUDED
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 283
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 1
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: need some help with C string

 
0
  #34
Jun 29th, 2009
yea basically he only wants me to use c-string and string. Here's a link to what he wants me to do. Maybe i am supposed to read the sentence as a string.
http://home.earthlink.net/~craie/122/labs/strfind.html
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #35
Jun 29th, 2009
I just checked and that function works.
And you can subsititute this line
  1. const char * Search::Find( const char pStr[], char c )

As I said, it too means a pointer to an array! Which is what an ASCIIz string is, and array of characters.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 283
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 1
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

Re: need some help with C string

 
0
  #36
Jun 29th, 2009
thanks for help but i haven't learnt about pointers so i need another way of doing the problem. Its okay though i will just ask him tom.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #37
Jun 29th, 2009
You're not exactly following the instructions.
They aren't asking you to use a string class.
They want you to create simple C code.

His comment of...
Two useful functions that are not provided in the standard library are find a character in a string and find a substring in a string.
...isn't exactly true. It does exist as strstr() and strchr() but they return a pointer to the location of the match within the string or NULL.
Your assignment is asking for you to return the index, or (-1) if not found!

A quick test is
  1. char *p;
  2. MyBuf[] <--- "Quick Brown Fox"
  3. char c = 'B';
  4.  
  5. p = strchr( MyBuf, c );
  6. if (NULL == p)
  7. return -1;
  8. else
  9. return (int)(p - MyBuf);

But only use that for cross checking.
You can do it yourself.
Last edited by wildgoose; Jun 29th, 2009 at 4:44 pm. Reason: typo
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #38
Jun 29th, 2009
Play with this...

  1. int Search::Find( char pStr[], char c )
  2. {
  3. int nCnt = 0;
  4.  
  5. while ( pStr[ nCnt ] ) // Loop until terminator found
  6. {
  7. if ( c == pStr[ nCnt ] ) // Match found?
  8. {
  9. return nCnt; // Return the index
  10. }
  11.  
  12. nCnt++;
  13. }
  14.  
  15. return -1; // Not found
  16. }
Last edited by wildgoose; Jun 29th, 2009 at 4:43 pm. Reason: Removed pointers.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 11
Reputation: smart_pc is an unknown quantity at this point 
Solved Threads: 1
smart_pc smart_pc is offline Offline
Newbie Poster

Re: need some help with C string

 
0
  #39
Jul 4th, 2009
see, first of all i dont know why u r using c++ header files in c.
but if u r using then u have said that u want to make two functions to find character and string . which u have not yet made in the program. i m not gettingwhat ur problem exactly is. clear it and i will help u . before using declare function. prototype declaration.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC