473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C++ Comparing character arrays

10
working on a 'hangman' type project. i need to know how to compare the character arrays so i can "return true", i.e. the word was correctly guessed, or false if they weren't.

below is what i have so far but all it does is compare individual cells and not the whole string.

any help would be awesome, and just FYI i am a beginning programmer.



Expand|Select|Wrap|Line Numbers
  1. bool matchTemplate ( const char secretWord[], const char guessTemplate[] )
  2. {
  3.  
  4.     //  will return true if secretWord and guessTemplate contain the same
  5.     //  C++ string and will return false otherwise
  6.  
  7.     //   ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION
  8.  
  9.  
  10.  
  11.     for ( int i = 0; i < MAX_SIZE; i++ )
  12.     {
  13.         if ( secretWord[i] == guessTemplate[i] )
  14.             return true;
  15.         else
  16.             return false;        
  17.     }
  18.  
  19. }
Jul 13 '08 #1
8 20724
JosAH
11,448 Expert 8TB
You can either use plain old C strings (the strcmp function would do the job for you)
or use C++ string objects (where the compare function would help you out).

kind regards,

Jos
Jul 13 '08 #2
mwhit74
10
[QUOTE
or use C++ string objects (where the compare function would help you out).

kind regards,

Jos[/quote]

ok i understand i have to compare the two strings, and i dont know how to convert the char arrays to strings
Jul 13 '08 #3
JosAH
11,448 Expert 8TB
ok i understand i have to compare the two strings, and i dont know how to convert the char arrays to strings
Are you programming in C or are you programming in C++? both languages
have their own solutions for 'strings'. C uses the notion of a simple character
array where the last character is a '\0' (just a byte/char with all bits zero) and
C++ uses the 'string' class for the representation of strings.

kind regards,

Jos
Jul 13 '08 #4
mwhit74
10
Are you programming in C or are you programming in C++? both languages
have their own solutions for 'strings'. C uses the notion of a simple character
array where the last character is a '\0' (just a byte/char with all bits zero) and
C++ uses the 'string' class for the representation of strings.

kind regards,

Jos
i am programming in C++. i am not sure if you are understanding the question i am getting at.... i have char arrays and i need them to be strings but i dont know how to convert from char arrays to strings so i can compare them.
Jul 13 '08 #5
JosAH
11,448 Expert 8TB
i am programming in C++. i am not sure if you are understanding the question i am getting at.... i have char arrays and i need them to be strings but i dont know how to convert from char arrays to strings so i can compare them.
Don't worry, I do understand ;-) There is a string constructor that takes an ordinary
char array and the number of chars that should be converted to a string object.
Have a look at this reference.

kind regards,

Jos
Jul 13 '08 #6
newb16
687 512MB
reference.
I don't think his prof will be happy with something like if(std::string(secretWord)== std::string(guessTemplate)) { ... }
It's op's own responsibility to understand how strings are represented as arrays and write loop-based strcmp routine.
Jul 14 '08 #7
oler1s
671 Expert 512MB
It's op's own responsibility to understand how strings are represented as arrays and write loop-based strcmp routine.
There's no evidence that this project has a "use only C strings and no cstring allowed" clause. Writing string handling routines is an excellent learning exercise, but I doubt this is it.
Jul 14 '08 #8
newb16
687 512MB
There's no evidence that this project has a "use only C strings and no cstring allowed" clause. Writing string handling routines is an excellent learning exercise, but I doubt this is it.
Then what is the following comment for in case of std::string allowed?
// ONLY ONE LOOP IS ALLOWED IN THIS FUNCTION
Jul 14 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
12
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
9
by: mahurshi | last post by:
i have a quick question i am putting a debug flag in my program (i really dont need this feature, but i figured it might be useful when i get into trouble) so i want to check if argv is the...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
8
by: Frost | last post by:
Hi All, I am a newbie i have written a c program on unix for line by line comparison for two files now could some one help on how i could do word by word comparison in case both lines have the...
1
by: Donald Grove | last post by:
If I have two arrays, what is a good paradigm for comparing what is in them, to determine what elements they share, or don't share? Specifically, each array could potentially contain the integers...
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.