Pointers and Characters

Hello! So here is my code:
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
52
53
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <string>
#include <algorithm>
#include "CS201Final.h"

using namespace 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)
    {
     return false;
    }
    
    else
    {
       return true;
    }

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.

Also, I am not allowed to use the cstring header.
Right now, you're always comparing your string characters to the character 'c'. The line should be:
if (ptr1[i] == c)

Also, your while loop conditions don't make any sense. You should be testing whether ptr1[i] and ptr2[i] are null terminator values, or '\0'.

If you can use <string> you should convert your char* to strings.

Also, don't use goto in your loop, just use break.
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
bool isPermutation( const char* ptr1, const char* 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 ) return false ; // lengths are different; can't be permutations
        if( count1 != count2 ) return false ; // counts are different; can't be permutations
    }

    return true ; // if we haven't hit any of the the earlier returns,
                   // counts must have matched for all chars
}

bool isPermutation2( const char* ptr1, const char* 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>
}
Yeah, I'm sure the entire point of the exercise was to use the <algorithm> header... /sarcasm
Last edited on
Topic archived. No new replies allowed.