Click here to Skip to main content
15,895,656 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
// samakhano0m.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
 
int main()
{
	int n , m ;
	int i = 0 , j = 0 ;
	
	cout << " Enter the lengh of string1 " << endl ;
	cin >> n ;
	cout << " Enter the lengh of string2 " << endl ;
	cin >> m ;
	char A[100];       char B[100];
	cout << "Enter the string1 : " << endl ;
	for( int j = 0 ; j < n ; j++ ){
	     cin >> A[j] ;
	}
	
	cout << "Enter the string2 : " << endl ;
	for( int i = 0 ; i < m ; i++ ){
	    cin >> B[i] ;
	}
	i=0;   j=0;   int r=0;
		for (j = 0;j< n-1 ;j++ )
			{
				if( B[i] == A[j] )
				{
					r++ ;
					if (i=m-1){
						i=0;
					}
					i++;
				}
				
				else
				{
					i = 0 ;
				}
		
		}
	cout << (r/m);
	
	
	return 0 ;
}

this program compare 2 string
a= xxxxxxyx
b=xy
but it does not ?
Posted
Updated 23-Apr-11 23:27pm
v2

It's clearly a homework question, even if not, a student would simply needs a 5 minute copy-pen to see what is happening here:
while( j < n )
{
  for( int i = 0 ; i < m ;  )
     for( int j = 0 ; j < n ; )
	if( B == A )
	{
	    i++ ;
	    cout << "yay"  ;
	}
	else
	{
            j++ ;
	}
}

It's pretty clear on what is happening here. You don't need a running environment to see/debug to find the issue and implementation. Further, why not write a new one by yourself instead of correcting it, if you have that option. I see lots of issues here... starting with, if two string lengths are different what is left to be compared?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Apr-11 22:25pm    
My 5,
--SA
Sandeep Mewara 24-Apr-11 1:41am    
Thanks SA
There's a ton of errors in that code, I'll only address the loop:

while( j < n )
{
  for( int i = 0 ; i < m ;  ) //<-- No brackets
    for( int j = 0 ; j < n ; ) //<-- No brackets 
      if( B == A ) //<-- Comparing pointers, not characters, in no way using indexes (so loops are worthless anyway)
      {
	i++ ;
	cout << "yay"  ;
      }
      else
      {
	j++ ;
      }
}
 
Share this answer
 
Comments
mbue 23-Apr-11 16:51pm    
Thats right. The variables i & j are declared 3 times. There is a potential possibility for buffer overflows without limit n & m to max 100 (buffer length). There must not be brackets for single statements, but this looks better. All that are not really errors but them make no sense at all.
Regards.
Sergey Alexandrovich Kryukov 23-Apr-11 22:26pm    
My 5. You surely don't need to eat a whole egg to tell it's bad.
--SA
How about not flooding this forum with questions, instead working on one thing, and actually working, reading your source material, reading online articles, and trying to learn C++ instead of trying to get us to fix it all for you ?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900