StrStr with pointers

Please help me, I'm struggling to wrap my head around pointers and my teacher just gave us a large program due by Apr 6 2015. I desperately need help.
I've been trying to write strstr using pointers and I just can't figure out what to do. This becomes even more of an issue since I also have to write other functions that will depend on it.

I'll post the code I have done and the assignment. The stuff I have aside from strstr is fine it works, and I'm sure I can write parts 6,7 and 8. But 5,9, 10 and 11 I'm just not sure.


Programming Exercise
Write a menu driven program that has the following menu:

1) Get a cString
2) Copy one cString into another (strcpy)
3) Concatenate one cString onto the Original cString (strcat)
4) Finds the length of a cString (strlen)
5) Finds the substring in a cString (strstr)
6) Reverse a cString (strrev)
7) Case insensitive cString compare (stricmp)
8) Delete a Portion of the String
9) Insert another String into the First String
10) Create a substring
11) Find Index of Sub-String
12) Exit
Explanation
Option 8:
This function changes the value of string by deleting num_char characters starting at position start.
Option 9:
This function changes the value of string by inserting insert_string starting at start position.
Option 10:
This function changes the value of the cString by making it a substring of the original cString. The substring starts at position start and includes num_char characters.
Option 11:
This function scans through cString to find the first occurrence of the substring. The function then returns the index within cString of the first character of the matched substring.
Stipulations
• Don’t use array notation, pointer notation only.
• Don’t use any predefined string functions.
• You may use any functions written for this exercise to complete other functions. Hint: You can use strcpy to easily write strcat.



#include <iostream> 
#include <string>

using std::cout;
using std::cin;
using std::endl;

const int SIZE = 42;

void MenuSelection(char * src_ptr, char * dest_ptr, int * counter_ptr, int * counter2_ptr);
void GetString(char * src_ptr, char * dest_ptr);
void StrCpy(char * src_ptr, char * dest_ptr);
void StrCat(char * src_ptr, char * dest_ptr);
void Strlen(char * src_ptr, int * counter_ptr);
void StrStr(char * src_ptr, char * dest_ptr, int * counter_ptr, int * counter2_ptr);

int main()
{
	char dest[SIZE] = { '\0' };
	char src[SIZE] = { '\0' };
	int counter = 0;
	int counter2 = 0;

	char * src_ptr = src;
	char * dest_ptr = dest;
	int * counter_ptr = &counter;
	int * counter2_ptr = &counter2;

	MenuSelection(src_ptr, dest_ptr, counter_ptr);

	return 0;
}
void MenuSelection(char * src_ptr, char * dest_ptr, int * counter_ptr, int * counter2_ptr)
{
	int choice = 0;

	while (choice != 12)
	{
		cout << "Options\n\n"
			<< "1.  Get a cString\n"
			<< "2.  Copy on cString into another (strcpy)\n"
			<< "3.  Concatenate one cString onto the Original cString (strcat)\n"
			<< "4.  Finds the length of a cString (strlen)\n"
			<< "5.  Finds the substring in a cString (strstr)\n"
			<< "6.  Revers a cString (strrev)\n"
			<< "7.  Case insensitive cString compare (stricmp)\n"
			<< "8.  Delete a Portion of the String\n"
			<< "9.  Insert another String into the First String\n"
			<< "10. Create a substring\n"
			<< "11. Find Index of Sub-String\n"
			<< "12. Exit\n\n";

		cout << "Select Option: ";
		cin >> choice;

		switch (choice)
		{
		case (1) :
		{
			GetString(src_ptr, dest_ptr);

			cout << "\nThe String you entered is: ";
			while (*src_ptr != NULL)
			{
				cout << *src_ptr++;
			}
			cout << "\n\n";

			break;
		}
		case (2) :
		{
			GetString(src_ptr, dest_ptr);
			StrCpy(src_ptr, dest_ptr);

			cout << "\nThe String you entered is: ";
			while (*src_ptr != NULL)
			{
				cout << *src_ptr++;
			}
			cout << "\n\nIt has been copied to the destination: ";
			while (*dest_ptr != NULL)
			{
				cout << *dest_ptr++;
			}
			cout << "\n\n";

			break;
		}
		case (3) :
		{
			for (int i = 0; i < 2; i++)
			{
				GetString(src_ptr, dest_ptr);

				StrCat(src_ptr, dest_ptr);
			}
			cout << "\n";
			while (*dest_ptr != NULL)
			{
				cout << *dest_ptr++;
			}
			cout << "\n\n";
			break;
		}
		case (4) :
		{
			*counter_ptr = 0;
			GetString(src_ptr, dest_ptr);
			Strlen(src_ptr, counter_ptr);
			cout << "\n";
			cout << "The string length is " << *counter_ptr;
			cout << "\n\n";

			break;
		}
		case (5) :
		{
			for (int i = 0; i < 2; i++)
			{
				GetString(src_ptr, dest_ptr);
				if (i == 0)
				{
					StrCpy(src_ptr, dest_ptr);
				}
			}
			StrStr(src_ptr, dest_ptr, counter_ptr, counter2_ptr);

			break;
		}
		case (6) :
		{
			break;
		}
		case (7) :
		{
			break;
		}
		case (8) :
		{
			break;
		}
		case (9) :
		{
			break;
		}
		case (10) :
		{
			break;
		}
		case (11) :
		{
			break;
		}
		case (12) :
		{
			break;
		}
		default:
		{
			cout << "\nInvalid option\n\n";
		}
		}
	}

}
void GetString(char * src_ptr, char * dest_ptr)
{

	cout << "\nEnter string: ";
	cin.ignore(cin.rdbuf()->in_avail());        
	cin.getline(src_ptr, SIZE - 2);
	cin.ignore(cin.rdbuf()->in_avail());

}
void StrCpy(char * src_ptr, char * dest_ptr)
{
	while (*dest_ptr++ = *src_ptr++);
}
void StrCat(char * src_ptr, char * dest_ptr)
{
	for (; *dest_ptr != '\0'; *dest_ptr++);

	StrCpy(src_ptr, dest_ptr);
}
void Strlen(char * src_ptr, int * counter_ptr)
{
	for (; *src_ptr++ != '\0'; (*counter_ptr)++);      // increment counter + 1 until src = NULL
}
void StrStr(char * src_ptr, char * dest_ptr, int * counter_ptr, int * counter2_ptr)
{
	int start = 0;
	int end = 0;
	int flag_count = 0;
	int start_dest = 0;

	Strlen(src_ptr, counter_ptr);
	for (; *dest_ptr++ != '\0'; (*counter2_ptr)++);

	for (int i = 0; i < *counter2_ptr || flag_count == *counter_ptr; i++)
	{
		if (toupper(*dest_ptr) == toupper(*src_ptr))
		{
			if (flag_count == 1)
			{
				start_dest = dest_ptr;   // Doesn't work?
			}
			flag_count++;
			dest_ptr++;
			src_ptr++;
		}
		else if (toupper(*dest_ptr) != toupper(*src_ptr))
		{
			start_dest = 0;
			flag_count = 0;
			dest_ptr++;
			src_ptr = 0;
		}	
	}

	if (flag_count == *counter2_ptr)
	{
		cout << "Substring starts at ";
	}
	else
	{
		cout << "Substring is not located in the string.\n\n";
	}

}


Last edited on
Pointers can be super hard when you first learn about them. Try watching this playlist see if it gives you a good idea, it did for me - https://www.youtube.com/playlist?list=PL2_aWCzGMAwLZp6LMUKI3cc7pgGsasm2_
Thanks TarikNeaj for the videos. Those will be helpful.

I just wrote my code for strstr. Was wondering if anyone could help me return the index of where the first character in the sub-string starts. Or can this even be re-written to do that at all?


        int start = 0;
	int end = 0;
	int flag_count = 0;                               //If a char matches a flag is raised
	int counter = 0;
	int counter2 = 0;

	char * start_dest = 0;
	
	counter = Strlen(src_ptr);                                  
	counter2 = Strlen(dest_ptr);

	for (int i = 0; i < counter2 && flag_count != counter; i++) 
	{
		if (toupper(*dest_ptr) == toupper(*src_ptr))
		{
			if (flag_count == 1)                     //When first character is matched set 
			{                                                // start destination
				start_dest = dest_ptr - 1;              
			}
			flag_count++;
			dest_ptr++;
			src_ptr++;
		
		}
		else if (toupper(*dest_ptr) != toupper(*src_ptr))
		{
			start_dest = 0;
			dest_ptr++;
			src_ptr = src_ptr - flag_count;   //Sub ptr count from flags it has been passed
			flag_count = 0;                        //Second character doesn't match reset
		}	
	}
	
	if (flag_count == counter)
	{
		cout << "\nSubstring starts at " << start_dest << "\n\n";
	}
	else
	{
		start_dest = nullptr;    //If sub strign not found return nullptr instead of value
		cout << "\nSubstring is not located in the string.\n\n";
	}
	return start_dest;
}
Topic archived. No new replies allowed.