hi there

whats wrong in here?!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  #include<iostream>
using namespace std;
char *chaf(char *str1, char *str2){
	char *st;
	char c;
	int n = 0;
	while(*str2 != '\0'){
		if(*str1 == *str2){
			c = *str2;
			st = &c;
		}
		else{
			str1++;
			str2++;
			}
	}
	return st;
}
main(){
	char ch1[] = "howudoing?";
	char ch2[] = "udo";
	char p;
	p = chaf(&ch1[0], &ch2[0]);
	cout << p;
	return 0;
	
}
most obviously main() main always returns an int int main()
Having a closer look, what are you trying to do? This looks bad in so many ways. Give a better description of what you are intending and ill mock up a revision.

EDIT: Are you trying to find the sub string "udo"?
Last edited on
im trying to make a pointer that will point a sub string that is appiring on str1, and if its not appiring there, it wont point a thing..
Does this meet your requirements?

EDIT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

int main()
{
	std::string string1 = "superman";
	std::string string2 = "yetti";
	char* pointertocharacter = NULL;

	int index = string1.find(string2);

	if (!(index == string1.npos))
	{
		pointertocharacter = &string1[index];
		std::cout << *pointertocharacter;
	}

	std::cin.get();
}
Last edited on
Here is a version that mimics how you were trying to do it.
Please study code and if you have questions feel free to ask.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>

using namespace std;

int FindSubString(char string1[], char string2[])
{
	for (int i = 0; string1[i] != '\0'; i++) // while string remains in bounds
	{
		for (int j = 0; string2[j] == string1[i+j]; j++) 
		// while both characters in each string are equal
		// contiue to check equallity of subsequent characters
		{
			if (string2[j+1] == '\0')
			// when the next character is a null terminator of second string you will know
			// second string is equal to the sub string starting at [i].
			{
				return i; // return the index where substring started
			}
		}
	}
	return -1;
}

int main()
{
	char string1[] = "howudoing?"; // first string of characters
	char string2[] = "udo"; // second string of characters
	char *p_Character = NULL; // pointer to a character initialized with NULL
	int index = FindSubString(string1, string2); 
	// I am passing the array in and not just the first element.
	// & address of operator is not needed as arrays are pointers to elements.
	if (index != -1)
	// checks for match of strings
	{
		p_Character = &string1[index]; 
		// assign the pointer to the memory address of character
		// at string1[index].

		cout << *p_Character; 
		// output value at address.	
	}
	return 0;

}
Last edited on
Topic archived. No new replies allowed.