help with a program

i have to write a program that checks the presence a string inside another
umm i wrote it but its not working it launches but it doesn't give any output
string
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
45
46
47
48
49
50
51
#include <iostream.h>
#include <stdio.h>
#define size 30
void check(char a[size],char b[size]);
int stringlength (char s[size]);
void main()
{
	char a[size],b[size];
   cout<<"Please Enter Your First String: ";
   gets(a);
   cout<<"Please Enter Your Second String: ";
   gets(b);
   check(a,b);
   getchar();
}
void check(char a[size],char b[size])
{
   int count=0;
   for(int i=0;i<stringlength(a);i++)
   	{
      	if(a[i]==b[0])
         	{
            	for(int j=1;j<stringlength(b);j++)
               {
               	while(a[i+j]==b[j])
                  {
                  	if(a[i+j]=='\0'||b[j]=='\0')
               			count++;
                  }
               }
            }
      }
  	if(count!=0)
   	{
			cout<<"String 2 is present in string 1";
         cout<<endl<<"it occured "<<count<<" times";
      }
   else if (count==0)
   	{
   		cout<<"String 2 is NOT present in string 1";
   	}
}
int stringlength (char s[size])
{
   int i=0,k=1;
	while(s[i]!='\0')
   	{
      	k++;
      }
   return k;
}

any help is welcome :/
i think the problem is with the check function...
Are you allowed to use the find function?

http://www.cplusplus.com/reference/string/string/find/
no string.h functions
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
#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
	string a;
    string b;
    size_t found;
   cout << "Please Enter Your First String: ";
   cin >> a;
   cout << "Please Enter Your Second String: ";
   cin >> b;
   cout << "A = " << a  << " B = " << b << endl;
   found=a.find(b);
   if (a==b)
   {
   cout << a << " and " << b << " are identical" << endl;
   }
   else
   if (found!=string::npos)
   {
   cout << "B = " << b << " Found in : " << a << endl;
   }
   else
   {
   cout << "B = " << b << " not found in : " << a << endl;
   }
}
Last edited on
Topic archived. No new replies allowed.