#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <string>
#include <algorithm>
#include "CS201Final.h"
usingnamespace std;
bool isPermutation(char * ptr1, char * ptr2) // creates a function to see if two arrays are permutations of eachother
{
int count1;
int count2;
for(char c ='a'; c<='Z'; c++)
{
count1 = 0;
count2 = 0;
int i=0;
while(&ptr1[i] != NULL && &ptr2[i] != NULL)
{
if(ptr1[i]=='c')
{
count1 = count1 + 1;
}
if(ptr2[i] =='c')
{
count2 = count2 + 1;
}
i++;
}
if(count1 != count2)
{
goto end1;
}
}
end1: if(count1 != count2)
{
returnfalse;
}
else
{
returntrue;
}
So my question is how to actually compare what the pointer is pointing at, to a character? Because what I am trying to do is accept two strings through the parameters, and then test if those two strings are permutations of each other.
bool isPermutation( constchar* ptr1, constchar* ptr2 ) // make const-correct
{
for( int c = CHAR_MIN ; c <= CHAR_MAX ; ++c ) // for each char in the charset
// (we use an int to avoid overflow on ++c)
{
int count1 = 0;
int count2 = 0;
// for every character in the c-string till we get to the terminating null character
int i = 0 ;
for( ; ptr1[i] != 0 ; ++i ) // ptr1[i] is the char at position i
{
if( ptr1[i] == c ) ++count1 ;
}
// at this point, i contains the length of the c-string
int j = 0 ; // likewise, for the cecond c-string
for( ; ptr2[j] != 0 ; ++j ) // ptr2[j] is the char at position j
{
if( ptr2[j] == c ) ++count2 ;
}
if( i != j ) returnfalse ; // lengths are different; can't be permutations
if( count1 != count2 ) returnfalse ; // counts are different; can't be permutations
}
returntrue ; // if we haven't hit any of the the earlier returns,
// counts must have matched for all chars
}
bool isPermutation2( constchar* ptr1, constchar* ptr2 )
{
int i = 0 ; // length of first string
for( ; ptr1[i] != 0 ; ++i ) ;
int j = 0 ; // length of second string
for( ; ptr2[j] != 0 ; ++j ) ;
return i==j && std::is_permutation( ptr1, ptr1+i, ptr2 ) ; // <algorithm>
}