8 Replies - 4141 Views - Last Post: 17 June 2012 - 03:09 PM Rate Topic: -----

#1 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

How to compare 2 Arrays and Return Array Index?

Posted 16 June 2012 - 06:51 PM

I've been trying to figure this out for hours, but i can't seem to figure it out...

Here are the two arrays i want to compare:

char matches[LOOP][21] = {
      "Abc", "OOP", "0123", "abc", "xyz", "CD-R", "RAM", "SS", "3.5", "<=>"
   }


char correct[LINES][41] = {
      "",
      "Welcome To OOP244",
      " !\"#$%&'()*+,-./0123456789:;<=>?@",
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
      "abcdefghijklmnopqrstuvwxyz{||||||||||}"
   };


I have tried using strncmp, strcmp, and strtr.

After finding a match, how would I return the "matches" array index it was found at?

E.g. "OOP" exists in correct[1] so when those two compare, it will = true, and the return value would be "1" because OOP exist in Array Index 1 of "matches".

Is This A Good Question/Topic? 0
  • +

Replies To: How to compare 2 Arrays and Return Array Index?

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: How to compare 2 Arrays and Return Array Index?

Posted 16 June 2012 - 07:03 PM

Show your code where you check for matches.
Was This Post Helpful? 0
  • +
  • -

#3 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: How to compare 2 Arrays and Return Array Index?

Posted 16 June 2012 - 07:18 PM

Heres the code i used for the following:

strncmp
strncmp (correct[i], matches[i], 3)


strcmp
strcmp (correct[i], matches[i]


strstr
char *found = strstr (correct[i], matches[i]


These all either return errors or compiles and does not work as intended.

This is just a small portion of the program i can show the entire program if it helps.

This post has been edited by Sushii: 16 June 2012 - 07:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: How to compare 2 Arrays and Return Array Index?

Posted 16 June 2012 - 07:55 PM

> This is just a small portion of the program i can show the entire program if it helps.

I did want a bit more context. Show the entire function or relevant block of code in your program responsible for working with the arrays and doing the matching.

Also, do you understand what strcmp does? It checks that each element of the string matches. For example, "OOP" and "OOP" match. "OOP" and "OOPS" don't. Is this what you wanted?
Was This Post Helpful? 0
  • +
  • -

#5 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: How to compare 2 Arrays and Return Array Index?

Posted 16 June 2012 - 08:20 PM

I didn't know that strcmp compares that strictly.

Here is the source files.

a2.main.cpp
#include <stdio.h>
#include <string.h>

#include "segment.h"

#define LOOP 10


int main( ) {
   const char core_data[ ][2000] = {
"0", /* should insert the null byte '\0' only */

"01010111 This sequence contains the letter 'W'       " /*  87 = 'W' */
"      r5 0 #! 1 1 gP 0 f66-:] 0 [ } 1 v4t^ 0 1 n>?:77" /* 101 = 'e' */
" junk ^*@&#^% 0110  more junk }{r;,<> 1100         2x" /* 108 = 'l' */
"                  0    1       1     0 0          011" /*  99 = 'c' */
"0  ^%jh  1 ]{) OVR 1 - 0 _! 9 + 1  118Y z(x pM1      " /* 111 = 'o' */
"This next group of 8 should be SKP skipped! 11111111 " /* skipped */
"*&^! 0  1  1  0 1     1 0     128673kjshd:L<    2938 " /* 109 = 'm' */
"      r5 0 #! 1 1 gP 0 f66-:] 0 [ } 1 v4t^ 0 1 n>?:77" /* 101 = 'e' */
"#!/bin/bash 00  1 00 00 0 g^b-ps 6@&^%               " /*  32 = ' ' */
"01010100 This sequence contains the letter 'T'       " /*  84 = 'T' */
"0  ^%jh  1 ]{) OVR 1 - 0 _! 9 + 1  118Y z(x pM1      " /* 111 = 'o' */
"#!/bin/bash 00  1 00 00 0 g^b-ps 6@&^%               " /*  32 = ' ' */
"---repeat--->0 1 0 0 1 1 1 1<------------RPT2--------" /*  79 = 'O' */
"0 1 0 1 0bbavac9872962  000&*************************" /*  80 = 'P' */
"a0b0c1dpe1f0g0h01ijklmnopqrstuvwxyz******************" /*  49 = '1' */
"00fg333                                110100>>>>>>>>" /*  52 = '4' */
"00fg333                                110100>>>>>>>>" /*  52 = '4' */
"                     0000                            ",/*  0 = '\0' */

"00011110 This char should not be inserted!           " /*  30 cntrl */
" 00011111 This char should not be inserted!          " /*  31 cntrl */
"  00100000                                           " /*  32 = ' ' */
"   00100001                                          " /*  33 = '!' */
"    00100010                                         " /*  34 = '"' */
"     00100011                                        " /*  35 = '#' */
"      00100100                                       " /*  36 = '$' */
"       00100101                                      " /*  37 = '%' */
"        00100110                                     " /*  38 = '&' */
"         00100111                                    " /*  39 = ''' */
"          00101000                                   " /*  40 = '(' */
"           00101001                                  " /*  41 = ')' */
"            00101010                                 " /*  42 = '*' */
"             00101011                                " /*  43 = '+' */
"              00101100                               " /*  44 = ',' */
"               00101101                              " /*  45 = '-' */
"                00101110                             " /*  46 = '.' */
"                 00101111                            " /*  47 = '/' */
"                  00110000                           " /*  48 = '0' */
"                   00110001                          " /*  49 = '1' */
"                    00110010                         " /*  50 = '2' */
"                     00110011                        " /*  51 = '3' */
"                      00110100                       " /*  52 = '4' */
"                       00110101                      " /*  53 = '5' */
"                        00110110                     " /*  54 = '6' */
"                         00110111                    " /*  55 = '7' */
"                          00111000                   " /*  56 = '8' */
"                           00111001                  " /*  57 = '9' */
"                            00111010                 " /*  58 = ':' */
"                             00111011                " /*  59 = ';' */
"                              00111100               " /*  60 = '<' */
"                               00111101              " /*  61 = '=' */
"                                00111110             " /*  62 = '>' */
"                                 00111111            " /*  63 = '?' */
"                                  01000000           " /*  64 = '@' */
"                                   00000000          ",/*   0 = '\0'*/

"                                    01000001         " /*  65 = 'A' */
"                                     01000010        " /*  66 = 'B' */
"                                      01000011       " /*  67 = 'C' */
"                                       01000100      " /*  68 = 'D' */
"                                        01000101     " /*  69 = 'E' */
"                                         01000110    " /*  70 = 'F' */
"                                          01000111   " /*  71 = 'G' */
"                                           01001000  " /*  72 = 'H' */
"                                            01001001 " /*  73 = 'I' */
"                                             01001010" /*  74 = 'J' */
"                                            01001011 " /*  75 = 'K' */
"                                           01001100  " /*  76 = 'L' */
"                                          01001101   " /*  77 = 'M' */
"                                         01001110    " /*  78 = 'N' */
"                                        01001111     " /*  79 = 'O' */
"                                       01010000      " /*  80 = 'P' */
"                                      01010001       " /*  81 = 'Q' */
"                                     01010010        " /*  82 = 'R' */
"                                    01010011         " /*  83 = 'S' */
"                                   01010100          " /*  84 = 'T' */
"                                  01010101           " /*  85 = 'U' */
"                                 01010110            " /*  86 = 'V' */
"                                01010111             " /*  87 = 'W' */
"                               01011000              " /*  88 = 'X' */
"                              01011001               " /*  89 = 'Y' */
"                             01011010                " /*  90 = 'Z' */
"                            01011011                 " /*  91 = '[' */
"                           01011100                  " /*  92 = '/' */
"                          01011101                   " /*  93 = ']' */
"                         01011110                    " /*  94 = '^' */
"                        01011111                     " /*  95 = '_' */
"                       01100000                      " /*  96 = '`' */
"                      00000000                       ",/*   0 = '\0'*/

"                     01100001                        " /*  97 = 'a' */
"                    01100010                         " /*  98 = 'b' */
"                   01100011                          " /*  99 = 'c' */
"                  01100100                           " /* 100 = 'd' */
"                 01100101                            " /* 101 = 'e' */
"                01100110                             " /* 102 = 'f' */
"               01100111                              " /* 103 = 'g' */
"              01101000                               " /* 104 = 'h' */
"             01101001                                " /* 105 = 'i' */
"            01101010                                 " /* 106 = 'j' */
"           01101011                                  " /* 107 = 'k' */
"          01101100                                   " /* 108 = 'l' */
"         01101101                                    " /* 109 = 'm' */
"        01101110                                     " /* 110 = 'n' */
"       01101111                                      " /* 111 = 'o' */
"      01110000                                       " /* 112 = 'p' */
"     01110001                                        " /* 113 = 'q' */
"    01110010                                         " /* 114 = 'r' */
"   01110011                                          " /* 115 = 's' */
"  01110100                                           " /* 116 = 't' */
" 01110101                                            " /* 117 = 'u' */
"01110110                                             " /* 118 = 'v' */
" 01110111                                            " /* 119 = 'w' */
"  01111000                                           " /* 120 = 'x' */
"   01111001                                          " /* 121 = 'y' */
"    01111010                                         " /* 122 = 'z' */
"     01111011                                        " /* 123 = '{' */
"      011111RPT10                                    " /* 124 = '|' */
"       01111101                                      " /* 125 = '}' */
"        00000000                                     ",/*   0 = '\0'*/

"                                      01000011       " /*  67 = 'C' */
"                                       01000100      " /*  68 = 'D' */
"               00101101                              " /*  45 = '-' */
"                                     01010010        " /*  82 = 'R' */
"                                        01001111     " /*  79 = 'O' */
"                                          01001101   " /*  77 = 'M' */
"        00000000                                     ",/*   0 = '\0'*/

"                                     01010010        " /*  82 = 'R' */
"                                    01000001         " /*  65 = 'A' */
"                                          01001101   " /*  77 = 'M' */
"        00000000                                     ",/*   0 = '\0'*/

"                                    01010011         " /*  83 = 'S' */
"                                      01000011       " /*  67 = 'C' */
"                                    01010011         " /*  83 = 'S' */
"                                            01001001 " /*  73 = 'I' */
"        00000000                                     ",/*   0 = '\0'*/

"                     00110011                        " /*  51 = '3' */
"                00101110                             " /*  46 = '.' */
"                       00110101                      " /*  53 = '5' */
"#!/bin/bash 00  1 00 00 0 g^b-ps 6@&^%               " /*  32 = ' ' */
"                                         01000110    " /*  70 = 'F' */
"          01101100                                   " /* 108 = 'l' */
"       01101111                                      " /* 111 = 'o' */
"      01110000    RPT2                               " /* 112 = 'p' */
"   01111001                                          " /* 121 = 'y' */
"        00000000                                     " /*   0 = '\0'*/

};

   char phrase[41];
   char correct[LOOP][41] = {
      "",
      "Welcome To OOP144",
      " !\"#$%&'()*+,-./0123456789:;<=>?@",
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
      "abcdefghijklmnopqrstuvwxyz{||||||||||}",
      "CD-ROM",
      "RAM",
      "SCSI",
      "3.5 Floppy",
      ""
   };

   char modified[LOOP][41] = {
      "",
      "Welcome To OOP244",
      " !\"#$%&'()*+,-./0123456789:;<=>?@",
      "aBCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
      "abcdefghijklmnopqrstuvwxyZ{||||||||||}",
      "CD_ROM",
      "ROM",
      "SCS",
      "3.5 Floppi",
      ""
   };

   char sorted[LOOP][41] = {
      "",
      "",
      " !\"#$%&'()*+,-./0123456789:;<=>?@",
      "3.5 Floppi",
      "CD_ROM",
      "ROM",
      "SCS",
      "Welcome To OOP244",
      "aBCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`",
      "abcdefghijklmnopqrstuvwxyZ{||||||||||}",
   };

   char matches[LOOP][21] = {
      "Abc", "OOP", "0123", "abc", "xyz", "CD-R", "RAM", "SS", "3.5", "<=>"
   }, gc;

   int mvalues[LOOP]  = { -1, 1, 2, 4, 4, 5, 6, -1, 8, 2 }, i;
   int cvalues[LOOP]  = { -1, 14, 999, 0, 25, 2, 1, 3, 9, 12000 };
   char chgs[LOOP] = { '\0', '2', '\0', 'a', 'Z', '_', 'O', '\0', 'i', '\0' };
   int sval[LOOP]     = { 0, 1, 0, 1, 1, 1, 1, 1, 1, 0 };
   int valid = 1, rc, pass=0;

   segment xf1;
   xf1.initialize(core_data, LOOP-1); // passing only 9 strings
   
   for(i=0; i<LOOP && valid; i++, pass++) {
      xf1.get_word(phrase, i);
      if(strcmp(phrase, correct[i])) {
         printf("\nFailed while testing the initialize( ) function...\n");
         printf("Failed on test... %d\n\n", pass+1);
         valid = 0;
      }
      else {
         printf("\nPassed test %d...\n\n", pass+1);
      }
      printf("Your string ----------> '%s'\n", phrase);
      printf("Actual string --------> '%s'\n", correct[i]);
      printf("Press the ENTER key to continue...");

      getchar( );
   }

   if(valid) {
      for(i=0; i<LOOP && valid; i++, pass++) {
         rc = xf1.match(matches[i]);
         if(rc != mvalues[i]) {
            printf("\nFailed while testing the match( ) function...\n");
            printf("Failed on test... %d\n\n", pass+1);
            valid = 0;
         }
         else {
            printf("\nPassed test %d...\n\n", pass+1);
         }
         printf("Your match( ) return value ----> %d\n", rc);
         printf("Actual match( ) return value --> %d\n", mvalues[i]);
         printf("Press the ENTER key to continue...");
         getchar( );
      }
   }
   if(valid) {
      for(i=0; i<LOOP && valid; i++, pass++) {
         rc = xf1.set_char(i, cvalues[i], chgs[i]);
         xf1.get_word(phrase, i);
         if(rc != sval[i] || strcmp(phrase, modified[i])) {
            printf("\nFailed while testing the set_char( ) function...\n");
            printf("Failed on test... %d\n\n", pass+1);
            valid = 0;
         }
         else {
            printf("\nPassed test %d...\n\n", pass+1);
         }
         printf("Your modified word ----------> '%s'\n", phrase);
         printf("Actual modified word --------> '%s'\n", modified[i]);
         printf("Press the ENTER key to continue...");
         getchar( );
      }
   }
   if(valid) {
      for(i=0; i<LOOP && valid; i++, pass++) {
         gc = xf1.get_char(i, cvalues[i]);
         if(gc != chgs[i]) {
            printf("\nFailed while testing the get_char( ) function...\n");
            printf("Failed on test... %d\n\n", pass+1);
            valid = 0;
         }
         else {
            printf("\nPassed test %d...\n\n", pass+1);
         }
         printf("Your character ----------> '%c'\n", gc);
         printf("Actual character --------> '%c'\n", chgs[i]);
         printf("Press the ENTER key to continue...");
         getchar( );
      }
   }
   if(valid) {
      xf1.sort( );
      for(i=0; i<LOOP && valid; i++, pass++) {
         xf1.get_word(phrase, i);
         if(strcmp(phrase, sorted[i])) {
            printf("\nFailed while testing the sort( ) function...\n");
            printf("Failed on test... %d\n\n", pass+1);
            valid = 0;
         }
         else {
            printf("\nPassed test %d...\n\n", pass+1);
         }
         printf("Your sorted string ----------> '%s'\n", phrase);
         printf("Actual sorted string --------> '%s'\n", sorted[i]);
         printf("Press the ENTER key to continue...");
         getchar( );
      }
   }

   if(valid) {
      printf("\nCongratualtions!!! You passed all tests.\n");
      printf("Your program is %.2lf%% complete!\n", 100.0*(pass)/50.0);
      printf("You may hand in your assignment.\n");
   }
   else {
      printf("\nYou passed %d/50 tests...\n", pass-1);
      printf("Your program is %.2lf%% complete!\n", 100.0*(pass-1)/50.0);
      printf("Your program still needs some work!\n");
      printf("Keep at it!\n\n");
   }
   return 0;
}


segment.cpp
#include <iostream>
#include <cstring>
#include <string>

#include "segment.h"
#include "assign1.h"

using namespace std;

void segment::initialize(const char core[ ][2000], int num) {
	int i;

	for (i = 0; i < num; i++) {
		bit_to_ascii(core[i], data[i]);
	}
	while (num < 10) {
		data[num][0] = '\0';
		++num;
	}
	/* Shows the data in data[] */
	for (int a = 1; a < 10; a++) {
	cout << data[a] << endl;
	}
	

}

void segment::initialize( ) {
	int i;

	for (i = 0; i < MAX_STRING; i++) {
		data[i][0] = '\0';
	}
}

int segment::match(const char word[ ]) {
	int i;

	for (i = 0; i < MAX_STRING; i++) {
          [color="#FF0000"] /* The comparison and array index return goes in here. */[/color]
		}
	
	return 0;
}

void segment::sort( ) {

}

void segment::get_word(char s[ ], int pos) {
	if (pos < MAX_STRING)
		strcpy(s, data[pos]);
	else
		s[0] = '\0';
}

int segment::set_word(const char s[ ], int pos) {

	return 0;
}

int segment::set_char(int word_pos, int char_pos, char value) const {

	return 0;
}

char segment::get_char(int word_pos, int char_pos) {

	return 0;
}


I dont think the header files are relevant in this case.

This post has been edited by Sushii: 16 June 2012 - 08:21 PM

Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3040
  • View blog
  • Posts: 10,619
  • Joined: 08-August 08

Re: How to compare 2 Arrays and Return Array Index?

Posted 16 June 2012 - 08:32 PM

View PostOler1s, on 16 June 2012 - 10:55 PM, said:

Also, do you understand what strcmp does? It checks that each element of the string matches. For example, "OOP" and "OOP" match. "OOP" and "OOPS" don't. Is this what you wanted?

Quoted for emphasis.

If you don't want an exact match you'll need want to use regular expressions. Either that or iterate through each string comparing substrings of the longer string to the shorter string.

Just noticed you're including C++ strings:
#include <string>

If you use those, then you could make use of find.

This post has been edited by CTphpnwb: 16 June 2012 - 08:42 PM

Was This Post Helpful? 0
  • +
  • -

#7 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: How to compare 2 Arrays and Return Array Index?

Posted 17 June 2012 - 02:46 AM

I tried using it, thats why it was there.

Whenever i use it though i get this error

error C2228: left of '.find' must have class/struct/union



I'm probably using it wrong, here is my code.

		size_t found; 
		found = data[i].find(word);

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6081
  • View blog
  • Posts: 23,557
  • Joined: 23-August 08

Re: How to compare 2 Arrays and Return Array Index?

Posted 17 June 2012 - 03:04 AM

Because data[i] is a C-string, and .find() works on C++ std::strings.

Maybe you want strstr(): http://www.cplusplus...cstring/strstr/
Was This Post Helpful? 0
  • +
  • -

#9 Sushii  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 11-December 08

Re: How to compare 2 Arrays and Return Array Index?

Posted 17 June 2012 - 03:09 PM

View PostJackOfAllTrades, on 17 June 2012 - 04:04 AM, said:

Because data[i] is a C-string, and .find() works on C++ std::strings.

Maybe you want strstr(): http://www.cplusplus...cstring/strstr/


Thanks after multiple attempts i seem to have figured it out!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1