| | |
need some help with C string
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
I'd recommend starting with a basic C book.
Did the teacher say no pointers?
Did the teacher say no pointers?
C++ Syntax (Toggle Plain Text)
char MyBuf[ 256 ]; const char *p; strcpy( MyBuf, "Happy Fourth of July"; p = a.Find( MyBuf, 'u' )) const char * Search::Find( const char *pStr, char c ) { while ( *pStr ) // Loop until terminator found { if ( c == *pStr ) // Match found? { return pStr; // Return the pointer } pStr++; } return (const char *)NULL; // Not found return NULL }
•
•
Join Date: Jun 2009
Posts: 283
Reputation:
Solved Threads: 1
most recent code
//main
//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
//main
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> using namespace std; #include "strextra.h" int main() { Search a; cout<<"enter max number of characters in the sentence"; int number; cin>>number; cout<<"enter a sentence"; string mycstring; cin>>mycstring; cout<<"my string is\n"<<mycstring<<"\n"; cout<<"enter the character you are trying to find in the string above\n"; char alpha; char lett[2]; cin>>alpha; lett[0]=alpha; lett[1]='\0'; cout<<lett[0]<<lett[1]; a.find(mycstring, lett[0]); }
[/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
C++ Syntax (Toggle Plain Text)
#ifndef STREXTRA_H_INCLUDED #define STREXTRA_H_INCLUDED #include <iostream> using namespace std; class Search { public: int find(string mystring,char letter[0]); int find(char mystring,string word); }; #endif // STREXTRA_H_INCLUDED
•
•
Join Date: Jun 2009
Posts: 283
Reputation:
Solved Threads: 1
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
http://home.earthlink.net/~craie/122/labs/strfind.html
I just checked and that function works.
And you can subsititute this line
As I said, it too means a pointer to an array! Which is what an ASCIIz string is, and array of characters.
And you can subsititute this line
C++ Syntax (Toggle Plain Text)
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.
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
But only use that for cross checking.
You can do it yourself.
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
C++ Syntax (Toggle Plain Text)
char *p; MyBuf[] <--- "Quick Brown Fox" char c = 'B'; p = strchr( MyBuf, c ); if (NULL == p) return -1; else 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
Play with this...
C++ Syntax (Toggle Plain Text)
int Search::Find( char pStr[], char c ) { int nCnt = 0; while ( pStr[ nCnt ] ) // Loop until terminator found { if ( c == pStr[ nCnt ] ) // Match found? { return nCnt; // Return the index } nCnt++; } return -1; // Not found }
Last edited by wildgoose; Jun 29th, 2009 at 4:43 pm. Reason: Removed pointers.
•
•
Join Date: Sep 2008
Posts: 11
Reputation:
Solved Threads: 1
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.
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: I need help with the errors I developed with C++
- Next Thread: fatal error LNK1120
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





