-2

How is it possible to see if the last four elements of an array match the last four elements of the second array?

For instance, I'm writing a password program. First, the user is asked their birth date. Then they are asked to input a password.

for the birth date array, the user enters '05/14/1984' for the password array, the user enters 'coke_1984'

I'm trying to see if the last four of birth date are the same as the last four of the password, if all four match, then add 1 to score.

I'm completely stuck! The code I have now has an "invalid conversion from 'char' to 'const char*'"

for(i = len; i <= len; i--)
{
    if(strcmp(birthdate, password[i]) == 0)
    {
        dateCMP = true;
    }else{dateCMP = false;}
}
if(dateCMP = true)
{
    score += 1;
    cout << "Your date of birth should not be the last four characters";
}
Quadruckus
  • 21
  • 4
  • this `for(i = len; i <= len; i--)` should be `for(i = len-4; i <= len; i--)` –  Nov 03 '15 at 08:03
  • 2
    Make your life easier use `std::string`. – 101010 Nov 03 '15 at 08:04
  • @GRC Where's the difference? – Simon Kraemer Nov 03 '15 at 08:05
  • I can't use strings for this assignment, should have mentioned that. I can only use arrays of chars. – Quadruckus Nov 03 '15 at 08:05
  • 3
    Check that loop condition. When will it become false? – Some programmer dude Nov 03 '15 at 08:05
  • I updated my comment :) –  Nov 03 '15 at 08:06
  • 1
    Also change `if(dateCMP = true)` to `if(dateCMP)`. – Henrik Nov 03 '15 at 08:07
  • 1
    As for your problem, I *assume* that the variable `password` is an array och `char`? Then think about what `password[i]` is, and what arguments [`strcmp`](http://en.cppreference.com/w/cpp/string/byte/strcmp) takes – Some programmer dude Nov 03 '15 at 08:07
  • 2
    Maybe look into the `std::equal` algorithm. – juanchopanza Nov 03 '15 at 08:08
  • `for(i = len; i <= len; i--)` is part of problem, this for loop wont even run –  Nov 03 '15 at 08:11
  • @GRC And why wouldn't it run? After the initialization phase of the for loop, 'i' has the value of 'len', so, the check of 'less than or equal to' is true, since it is equal to 'len'. And after the loop 'i' is decreased. On the next iteration the check, effectively becomes ('len' - 1 <= 'len'). Which is trivial, since any number - 1 would be less then the same number. And the same kind of pattern would continue for eternity. The problem with that loop isn't that it won't run. It is that it will loop infinitely. – Algirdas Preidžius Nov 03 '15 at 08:57

4 Answers4

3

Make your life easier and take advantage of the already provided STL facilities like std::string.

Below is a function that takes as entries 2 strings and returns true if their last 4 characters are equal and false otherwise:

bool
compare_last_4_characters(std::string const &str1, std::string const &str2) {
  std::size_t sz1 = str1.size();
  std::size_t sz2 = str2.size();
  if(sz1 > 3 && sz2 > 3) {
    return str1.substr(sz1 - 4, sz1) == str2.substr(sz2 - 4, sz2);
  }
  return false;
}

LIVE DEMO

If you can't use std::string below is a version that works without them:

bool
compare_last_4_characters(char const *str1, char const *str2) {
  int sz1 = strlen(str1) - 4;
  int sz2 = strlen(str2) - 4;  
  if(sz1 >= 0 && sz2 >= 0) return !strcmp(str1 + sz1, str2 + sz2);
  return false;
}

LIVE DEMO

101010
  • 41,839
  • 11
  • 94
  • 168
  • I can't use string for this project, only arrays of characters. =( – Quadruckus Nov 03 '15 at 08:25
  • I figured it out with arrays. I just made an array called compare to store values of password if password[i] == birthdate[i]... then used an if to strcmp(compare, birthdate)... for(i = len-4; i < len; i++) { if(birthdate[i] == password[i]) { compare[i] = password[i]; } } if(strcmp(compare, birthdate)) { weakness += 1; } – Quadruckus Nov 03 '15 at 08:34
0
#include <iostream>
#include <utility>
#include <algorithm>

//size_t literal: see http://stackoverflow.com/questions/22346369/initialize-integer-literal-to-stdsize-t
constexpr std::size_t operator "" _z(unsigned long long n)
{
    return n;
}

bool lastFourEquals(const char* str1, size_t strlen1, const char* str2, size_t strlen2)
{
    //variant 1: do not allow shorter strings
    //if(strlen1 < 4 || strlen2 < 4) return false;

    //Variant 2: maximum of last for equals (e.g. "123"=="0123")
    size_t maxLen =std::min(std::max(strlen1, strlen2), 4_z);

    for (int i = 1; i <= maxLen; i++)
    {
        size_t pos1 = strlen1 - i;
        size_t pos2 = strlen2 - i;
        if (str1[pos1] != str2[pos2]) return false; //Found difference
    }

    return true;
}

int main()
{
    const char* test1 = "05/14/1984";
    const char* test2 = "coke_1984";

    bool eq = lastFourEquals(test1, strlen(test1), test2, strlen(test2));

    std::cout << (eq ? "true" : "false") << std::endl;
}
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49
-1

I figured it out by using another array. I just made an array called compare to store values of password if password[i] == birthdate[i]... then used an if to strcmp(compare, birthdate)...

for(i = len-4; i < len; i++)
{
    if(birthdate[i] == password[i])
    {
        compare[i] = password[i];
    }
}
if(strcmp(compare, birthdate))
{
    score += 1;
}

Thanks for your attempts to help!

Quadruckus
  • 21
  • 4
-1

I think this works! I finally got it. It's a pain in the ass not being able to use the string library. Please let me know! You guys have been so much help. Don't worry about my other arguments in the function, I'm not using those quite yet.

//this function runs to see if the first letter of last name matched
//first letter of password, if the birthdate

int checkStrength(char password[SIZE], char lastName[SIZE], char 
birthdate[SIZE], char carMake[SIZE], int favNum)
{

//array and variables for use through the checks
char compareDate[SIZE];
int weakness = 0;
int len = strlen(password) - 4;
int i;
int c = 0;

//this for loop compares arrays, if comparison is found, adds element to
//compareDate and then uses an if statement to strcmp compare and birthdate
//if compare and birthdate are then the same, add weakness
for(i = 0; i < len; i++)
{
    if(birthdate[c] == password[len])
    {
        compareDate[c] = password[i];
        c += 1;
    }
}
if(strcmp(compareDate, birthdate) == 1)
{
    weakness += 1;
    cout << "Your birth year should not be the last four character of your 
    password.";
    cout << compareDate;
}

//this checks to see if the car make array is used in order through password,
//if it is, add weakness


return weakness;

}
Quadruckus
  • 21
  • 4
  • Multiple issues with this solution: *1)* `len` should be of type `size_t` *2)* `compareDate` is not null terminated. *3)* Using `"1984coke_"` as password and `"05/14/1984"` as birtdate declares the password is too week. *4)* So does `"1"` as password. *Summary* It just doesn't do what it should. – Simon Kraemer Nov 03 '15 at 10:44
  • And you should better pass arrays by pointer or by reference instead of copying them around. Maybe even declare them as const as you don't alter any of them. – Simon Kraemer Nov 03 '15 at 10:46