GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Nov-2004, 00:02
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road

I am reviewing Arrays and need help converting some strings to arrays


I am reviewing Arrays and need help converting some strings to arrays in this script. All the places in red are places where I want to create Array functions instead of calling a string. For example, When you enter the word you want to use I want to make that a scanf and place the word into an array. I know it is not the most effective way to write this but my professor does not want us tosing use strings yet and I am not sure how to do this without using strings. Any hints?

#include <stdio.h>
#include <stdlib.h>


#define MAXLEN 26
#define MAX 10
#define YES 1
#define NO 0
#define STARS "********************"

int main (void)
{
char word [MAXLEN + 1],
solution [MAXLEN + 1],
ch,
*w_ptr,
*s_ptr;
int num_guess = 0,
len = 0,
found = 0;

printf("\nINSTRUCTIONS: ");

do
{ num_guess = 0;
printf("\nEnter the word for the player to guess and end with * > ");
fgets(word, MAXLEN, stdin);


for( w_ptr = word ; *w_ptr ; w_ptr++)
*w_ptr = toupper(*w_ptr);

len = strlen(word);
strncpy(solution, STARS, len); solution[len] = '\0';

while( num_guess < MAX )
{ printf("\n\nWord so far is %s, Guesses so far %d"
,solution ,num_guess++);
printf("\nEnter a letter > ");
ch = toupper(getche());
s_ptr = solution;
w_ptr = word;
found = NO;

while ( *w_ptr )
{ if ( ch == *w_ptr )
{ *s_ptr = *w_ptr;
found = YES;
}
w_ptr++;
s_ptr++;
}

if ( strcmp(solution , word) == 0 )
break;
if ( found )
printf("\nThe letter \"%c\" was in the answer",ch);
}

if ( num_guess < MAX )
printf("\nYou got it! The word was %s.\nIt took you %d tries \n",word , num_guess);
else
printf("\nYou had %d tries, the word was %s\n", MAX ,word);
printf("\nPlay again [Y/N] - ");
}while( (toupper(getche())) == 'Y' ) ;
printf("\nGoodbye\n");
return EXIT_SUCCESS;
}
  #2  
Old 12-Nov-2004, 06:46
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi jenmaz. I will not harp too much on you for not using code tags, as you wanted to highlight some lines. However, it would be easier to read your code if you put it in code tags and then highlighted the lines in question with a comment like:
CPP / C++ / C Code:
// <--- HERE!!

Anyway, the basis of your question confuses me. In C (not C++) the term string is relative. C does not provide a type definition for string. So a "string" in C is really only an array of chars. Whether it is defined dynamically or statically, it is still the same thing.

Is it possible that your professor is saying that you can not use the C++ string type? I am not sure of any efficient and non complicated way to do your program with out using a char* or char[]. Maybe if you could show an example of what your storage type should be?
  #3  
Old 12-Nov-2004, 07:07
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
Hi,

When some types in a word like house. That should get stored in an array. Each letter would become an element of the array. Then when people quess a letter and it is not h o u s or e the letter they typed would go into another array and counted as WRONG. (only get 6 wrong quesses) If, for example, the user types in h that is stored in Array that is CORRECT.

So for the outer loop I am asking the first user to enter a word
CPP / C++ / C Code:
do
{ num_guess = 0;
printf("\nEnter the word for the player to guess and end with * > ");
fgets(word, MAXLEN, stdin);  //instead of this I would like to use a scanf statement that places the users word into a the char word. Using the stdin sting function will work and is the  most efficient way of writing it but there is a way to write it using just ARRAYS and no library STRING functions.
Hope this makes sense

Jennifer






Quote:
Originally Posted by dsmith
Hi jenmaz. I will not harp too much on you for not using code tags, as you wanted to highlight some lines. However, it would be easier to read your code if you put it in code tags and then highlighted the lines in question with a comment like:
CPP / C++ / C Code:
// <--- HERE!!

Anyway, the basis of your question confuses me. In C (not C++) the term string is relative. C does not provide a type definition for string. So a "string" in C is really only an array of chars. Whether it is defined dynamically or statically, it is still the same thing.

Is it possible that your professor is saying that you can not use the C++ string type? I am not sure of any efficient and non complicated way to do your program with out using a char* or char[]. Maybe if you could show an example of what your storage type should be?
Last edited by dsmith : 12-Nov-2004 at 07:15. Reason: Please use [c] & [/c] for syntax highlighting
  #4  
Old 12-Nov-2004, 07:25
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Jennifer. Can you please start using [c] and [/c] when you post your code? It really helps to view your code.

My recommendation: do not use scanf (unless your professor requires it). Your fgets is loading every char that is typed into an array. That is the definition of a string in C. Just because you use scanf, your string will still be stored in the exact same manner, which is exactly what you want (an array of chars)

I believe what your prof. is getting at is don't use the C string library (ie strcpy, etc.), but use char arrays instead to do this. So for a copy you could use:

CPP / C++ / C Code:
i = 0;
while(source[i]){
  dest[i] = source[i];
  i++;
}

I apologize if this doesn't seem to answer your question, but the only way to store a string in C is to use an array (either dynamic or static). You could (if you were crazy) store it in a linked list I guess, but that would be pretty involved.
  #5  
Old 12-Nov-2004, 07:53
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road

Thanks this helps


Hi there,

This is exactly what the prof wants us to do. But I am not in understanding of what you place as copy code loop. What would source[i] and dest [i] represent in relation to my code that is existing?

Quote:
Originally Posted by dsmith
Hi Jennifer. Can you please start using [c] and [/c] when you post your code? It really helps to view your code.

My recommendation: do not use scanf (unless your professor requires it). Your fgets is loading every char that is typed into an array. That is the definition of a string in C. Just because you use scanf, your string will still be stored in the exact same manner, which is exactly what you want (an array of chars)

I believe what your prof. is getting at is don't use the C string library (ie strcpy, etc.), but use char arrays instead to do this. So for a copy you could use:

CPP / C++ / C Code:
i = 0;
while(source[i]){
  dest[i] = source[i];
  i++;
}

I apologize if this doesn't seem to answer your question, but the only way to store a string in C is to use an array (either dynamic or static). You could (if you were crazy) store it in a linked list I guess, but that would be pretty involved.
  #6  
Old 12-Nov-2004, 08:05
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Okay Jen. At least I know what i am trying to do now

Can you (or do you want to) use functions?

If not, take this line for instance:
CPP / C++ / C Code:
strncpy(solution, STARS, len);

First there is no need to use strncpy, because the string literal is null terminated. So in this case, solution would be the destination & STARS would be the source.

CPP / C++ / C Code:
i = 0;
while(STARS[i]){
  solution[i] = STARS[i];
  i++;
}
solution[i] = 0;   //The dest. needs to be null terminated, because we didn't run through the loop last time.

The reason, I ask about functions is that this would make a nice little function (your own strcpy function if you will)

CPP / C++ / C Code:
int copy_string(char* dest, char* src)

Just a thought...
  #7  
Old 12-Nov-2004, 08:10
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
Thanks I am going to try this and see what happens. We are suppose to create functions but my thoughts were to understand how it works then clean up the code by creating functions.


Quote:
Originally Posted by dsmith
Okay Jen. At least I know what i am trying to do now

Can you (or do you want to) use functions?

If not, take this line for instance:
CPP / C++ / C Code:
strncpy(solution, STARS, len);

First there is no need to use strncpy, because the string literal is null terminated. So in this case, solution would be the destination & STARS would be the source.

CPP / C++ / C Code:
i = 0;
while(STARS[i]){
  solution[i] = STARS[i];
  i++;
}
solution[i] = 0;   //The dest. needs to be null terminated, because we didn't run through the loop last time.

The reason, I ask about functions is that this would make a nice little function (your own strcpy function if you will)

CPP / C++ / C Code:
int copy_string(char* dest, char* src)

Just a thought...
  #8  
Old 12-Nov-2004, 08:29
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
ok, I understand the copy code. I have not used the function yet. If you take this CODE below and execute it you will see that it is not doing what I need it to do.

CPP / C++ / C Code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
 


#define MAXLEN 26  
#define MAX 10
#define YES 1
#define NO 6
#define STARS "********************"

int main (void)
{
	char 	word [MAXLEN],  //Word being guessed
			solution [MAXLEN],
			ch,
			*w_ptr,
			*s_ptr;
	int 	num_guess = 0,
			len =  0,
			found = 0,
			i=0;


printf("\nINSTRUCTIONS: ");

	do
	{	
		
		printf("\nEnter the word for the player to guess and end with * > ");
        scanf(" %c", &word [MAXLEN]); //changed this from this --> fgets(word, MAXLEN, stdin);


			
			for( w_ptr = word ; *w_ptr ; w_ptr++)
			*w_ptr = toupper(*w_ptr);
		 	*w_ptr = tolower(*w_ptr); 

			i = 0;
			while(STARS[i]){
			solution[i] = STARS[i];
			i++;
			}
			solution[i] = 0; 

/*		len = strlen(word);
		strncpy(solution, STARS, len);
		solution[len] = '\0'; */


		while( num_guess < MAX )
		{	
			printf("\n\nWord so far is %c, Guesses so far %d" ,solution ,num_guess++);
			printf("\nEnter a letter > ");
			ch = toupper(getche());
			s_ptr = solution;
			w_ptr = word;
			found = NO;

			while ( *w_ptr )
			{	if ( ch == *w_ptr )
				{	*s_ptr = *w_ptr;
					found = YES;
				}
				w_ptr++;
				s_ptr++;
			}

			if (solution == 0 )
				break;
			if ( found )
				printf("\nThe letter \"%c\" was in the answer",ch);
		}

		if ( num_guess < MAX )
			printf("\nYou got it! The word was %c.\nIt took you %d tries \n", word [MAXLEN], num_guess);
		else
			printf("\nYou had %d tries, the word was %c\n", MAX ,word);
		printf("\nPlay again [Y/N] - ");
	}while( (toupper(getche())) == 'Y');
	printf("\nGoodbye\n");
	return EXIT_SUCCESS;
}





Quote:
Originally Posted by dsmith
Okay Jen. At least I know what i am trying to do now

Can you (or do you want to) use functions?

If not, take this line for instance:
CPP / C++ / C Code:
strncpy(solution, STARS, len);

First there is no need to use strncpy, because the string literal is null terminated. So in this case, solution would be the destination & STARS would be the source.

CPP / C++ / C Code:
i = 0;
while(STARS[i]){
  solution[i] = STARS[i];
  i++;
}
solution[i] = 0;   //The dest. needs to be null terminated, because we didn't run through the loop last time.

The reason, I ask about functions is that this would make a nice little function (your own strcpy function if you will)

CPP / C++ / C Code:
int copy_string(char* dest, char* src)

Just a thought...
  #9  
Old 12-Nov-2004, 08:54
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Jen.

Use a printf statement right after the string copy code and you should see that it works (it does for me at least ).

As for the rest of the program, I haven't looked to closely. (mostly because of the use of the non-standard getche).

Are you satisfied that the string copy is working and just asking about the rest or are you struggling with it still?
  #10  
Old 12-Nov-2004, 09:13
jenmaz jenmaz is offline
Junior Member
 
Join Date: Oct 2004
Posts: 38
jenmaz is on a distinguished road
Yes still strugling withthis I placed printf("%c", solution[i]); after the new code and it did not work.

I am so new and a little over whelmed.

I may be thinking too much on this.

This is actually due at 4PM today I need to upload this.

I need to know this for an exam in 2 weeks.

HELP!!!

jen

Quote:
Originally Posted by dsmith
Hi Jen.

Use a printf statement right after the string copy code and you should see that it works (it does for me at least ).

As for the rest of the program, I haven't looked to closely. (mostly because of the use of the non-standard getche).

Are you satisfied that the string copy is working and just asking about the rest or are you struggling with it still?
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:02.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.