Monday, January 31, 2011

Pointers and Arrays in C++

Comparing string arrays using pointers.
If this program makes errors please let me know,i will try to help you out..
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int mycomp(char *,char *);
main()
{
      const int arraysize=50;
      char a[arraysize],b[arraysize];
     
      int l;
      char *p,*p1;
      p=a;
      p1=b;
      cout<<"Enter first string:";
      gets(a);
      cout<<"Enter second string:";
      gets(b);
      l=mycomp(p,p1);
      if(l==0)
      cout<<"Strings are equal";
      else
      cout<<"Strings are not equal";
      getch();
      return 0;
      }
int mycomp(char *a,char *b)
{
      int flag=0;
      if (strlen(a)!=strlen(b))
      flag=1;
      else
      {
          int i=0;
      int j=0;
      while(a[i]!='\0')
      {
      if(a[i++]!= b[j++])
      {
                        flag=1;
                        break;
                        }
                        }
                        }
                        return flag;
                        }

No comments:

Post a Comment