0

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.

0

I'd recommend starting with a basic C book.
Did the teacher say no pointers?

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
}
0

most recent code

//main

#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]);
}

//implementation

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#include "strextra.h"

int search::find(string mystring,char letter[0])
{
    return strchr(mystring,letter);
}

//interface

#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

Edited by Reverend Jim: Fixed formatting

0

I just checked and that function works.
And you can subsititute this line

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.

0

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.

0

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

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.

0

Play with this...

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 
}
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.