question about arrays of strings

This is a discussion on question about arrays of strings within the C Programming forums, part of the General Programming Boards category; Hey, So I want to compare a part of a string with a word, but the part of the string ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    34

    question about arrays of strings

    Hey,
    So I want to compare a part of a string with a word, but the part of the string is in an array of characters. How would i send a part of this or just a single string to strcmp() ? Also, i dont understand why something like this doesnt work:
    Code:
    #define ACROSS 10     /* 10 by 10 wordsearch */
    #define COLUMNS 10
    
    char wordsearch[ACROSS][COLUMNS] 
    int i;
    char temp[10];
    
    	for ( i = 0 ; temp[i] != '\0' , i < 10 ; i++ )
    	{
    		temp[i] = wordsearch[0][i];
    	}
    
    	printf("%s",temp);
    /*Then i could compare temp to the word somehow... but i dont know how i could break it up */
    In the end, i basically want a program that will be able to find a given word in the wordsearch and give the first and last subscript of the word in the wordsearch.
    Thanks guys!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <stdio.h>
    #include <stddef.h>
    #include <string.h>
    
    int main ( void )
    {
      char input[10];
      char words[] = "These are a bunch of words";
      char *start, *end;
    
      printf ( "Enter a word to search for: " );
      gets ( input ); /* For simplicity */
    
      start = strstr ( words, input );
      
      if ( start != NULL ) {
        end = start + strlen ( input ) - 1;
    
        printf ( "Found \"%s\" starting at [%lu] and ending at [%lu]\n",
          input, (unsigned long)(start - words), (unsigned long)(end - words) );
      }
      else
        printf ( "\"%s\" not found\n", input );
    
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Prelude
    printf ( "Enter a word to search for: " );
    gets ( input ); /* For simplicity */


    -Prelude
    You realize that you will never, ever live this down, don't you?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You realize that you will never, ever live this down, don't you?
    Yes, but seeing Hammer have a heart attack will be worth it.

    -Prelude
    My best code is written with the delete key.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Yes, but seeing Hammer have a heart attack will be worth it.
    Thanks, right on Christmas too Are you on the merry juice or something?


    << damn gets() promoters need teaching a lesson, hurumph... mubble mubble >>
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    thanks prelude,
    although I just wanted to know if i had a wordsearch in a 2d character array, could i break it up into line by line.

    Code:
    /*this is my wordsearch */
    DKGUWOGAME
    MOUNTAINEO
    ALEVELFDAE
    KABDHCRAES
    EUEAAFGFUU
    AGDRKDFGRR
    AHDDEERPEE
    EIPROGRAMZ
    WMEASUREDO
    ASODIFEHEO
    
    /*how could i break it up into say the first row and column...*/
    /*eg*/
    
    DKGUWOGAME...then search for word in that,
    DMAKEEAAEWA...then search in that
    etc...etc... creating every column and row into a temporary string.
    haha, this isnt code at all.
    I was fooling around with something that would search for diagonals but then i realized my brain couldnt function.
    THANKS guys!

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Ah, a word search game type thing? Calculating down and across is very simple, as is down/right diagonal. Here's some crude quickie code that does what you want. It only handles three directions, left to right across, up to down, and left to right diagonal. The error handling also has much to be desired. If you need more you should have no problem adding it. Please excuse the crudity of the code.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    enum { ACROSS, DOWN, DIAG };
    
    void brkstr ( char **a, char *buf, int row, int col, int dir )
    {
      char *p = buf;
      
      while ( a[row][col] != '\0' ) {
        if ( dir == ACROSS )
          *p++ = a[row][col++];
        else if ( dir == DOWN )
          *p++ = a[row++][col];
        else if ( dir == DIAG )
          *p++ = a[row++][col++];
        else
          ;
      }
    }
    
    int find_match ( char *a, char *find, int *x, int *y )
    {
      char *start;
    
      start = strstr ( a, find );
    
      if ( start != NULL ) {
        *x = start - a;
        *y = ( start + strlen ( find ) - 1 ) - a;
    
        return 1;
      }
    
      return 0;
    }
    
    int main ( void )
    {
      int x, y;
      char *across = "PROGRAM";
      char *down   = "MAKE";
      char *diag   = "DAD";
      char holder[11] = {0};
    
      char *wordsearch[] = {
        "DKGUWOGAME",
        "MOUNTAINEO",
        "ALEVELFDAE",
        "KABDHCRAES",
        "EUEAAFGFUU",
        "AGDRKDFGRR",
        "AHDDEERPEE",
        "EIPROGRAMZ",
        "WMEASUREDO",
        "ASODIFEHEO",
        "\0\0\0\0\0\0\0\0\0\0", /* For finding down && diag */
      };
    
      brkstr ( wordsearch, holder, 7, 0, ACROSS );
    
      if ( find_match ( holder, across, &x, &y ) != 0 ) {
        printf ( "Found \"%s\" at [%d], ending at [%d]\n", across, x, y );
      }
    
      brkstr ( wordsearch, holder, 0, 0, DOWN );
    
      if ( find_match ( holder, down, &x, &y ) != 0 ) {
        printf ( "Found \"%s\" at [%d], ending at [%d]\n", down, x, y );
      }
    
      brkstr ( wordsearch, holder, 0, 0, DIAG );
    
      if ( find_match ( holder, diag, &x, &y ) != 0 ) {
        printf ( "Found \"%s\" at [%d], ending at [%d]\n", diag, x, y );
      }
      
      printf ( "\nEnter any 11-digit prime number to continue..." );
      getchar();
      
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    ok so this is what i have so far, but my diagonal most certainly does not work...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define ROWS 10
    #define COLUMNS 10
    
    void assignSearch( FILE *wrdsearch , char wordsearch[ROWS][COLUMNS] );
    int findWord( char word[], char wordsearch[][COLUMNS] );
    int findDiag ( char wordsearch[][COLUMNS] , char input[] );
    
    int main (void)
    {
    
    
    	FILE *wrdlist, *wrdsearch;
    	char input[11];
    	char wordsearch[ROWS][COLUMNS];
    	int test , notfound;
    
    
    
    	if((wrdsearch = fopen("wordsearch.dat","r")) == NULL)
    		printf("file failed to open");
    		
    	if((wrdlist = fopen("wordlist.dat","r")) == NULL)
    		printf("file failed to open");
    
    	assignSearch(wrdsearch,wordsearch);
    
    	printf("\n\nenter a word\n");
    	gets(input);
    
    	test = findWord(input,wordsearch);
    
    	if( test != 0)
    		findDiag(wordsearch,input);
    
    
    
    
    	return 0;
    }
    
    
    void assignSearch( FILE *wrdsearch , char wordsearch[ROWS][COLUMNS] )
    {
    	int i , j;
    
    	for( j = 0; j < COLUMNS ; j++ )
    	{
    		for ( i=0 ; i <= ROWS ; i++)
    		{
    			wordsearch[i][j] = fgetc(wrdsearch);
    		}
    	}
    
    	
    	for( j = 0; j < COLUMNS ; j++ )
    	{
    		for ( i=0 ; i <= ROWS ; i++)
    		{
    			printf("%c",wordsearch[i][j]);
    		}
    	}
    }
    
    
    int findWord( char input[], char wordsearch[][COLUMNS] )
    {
    	int i , j , m , k;
    
    	char tempd[11];
    	char tempdback[11];
    	char tempa[11];
    	char tempaback[11];
    
    	
    	char *start , *end;
    
    	printf("\n\n\n");
    
    	
    	for ( i = 0 ; i < ROWS ; i ++ )
    	{
    		for( j = 0 ; j < COLUMNS ; j++ )
    		{	
    			/************************************ down ********************************************/
    			tempd[j] = wordsearch[i][j];
    
    
    			start = strstr (tempd,input);
    
    				if( start!=NULL)
    				{
    					end = start + strlen(input) - 1;
    
    					printf("starts at [%i,%i], ends at [%i,%i]\n",i,(start - tempd),i,(end-tempd));
    					return 0;
    					break;
    						
    				}
    				else
    					;
    			/**************************************************************************************/
    
    
    			/********************************* down backwards (up) ********************************/
    			for(k=0 , m = 10 ; k<ROWS , m >= 0 ; k++ , m--)
    			{
    					tempdback[k] = tempd[m];
    			}
    
    
    			start = strstr(tempdback,input);
    
    			if( start!=NULL)
    				{
    					end = start + strlen(input) - 1;
    
    					printf("starts at [%i,%i], ends at [%i,%i]\n",
    						i, (COLUMNS - (start - tempdback)),i,(COLUMNS - (end-tempdback)));
    					return 0;
    					break;
    						
    				}
    				else
    					;
    			/**************************************************************************************/
    
    			
    			/******************************** across **********************************************/
    			tempa[j] = wordsearch[j][i];
    
    			start = strstr (tempa,input);
    
    				if ( start != NULL )
    				{
    					end = start + strlen(input) - 1;
    
    					printf("starts at [%i,%i], ends at [%i,%i]\n",
    						(start - tempa),i,(end - tempa),i);
    					return 0;
    					break;
    				}
    
    				else
    					;
    			/**************************************************************************************/
    
    
    			/****************************** across backwards **************************************/
    			for(k=0 , m = 10 ; k<ROWS , m >= 0 ; k++ , m--)
    			{
    					tempaback[k] = tempa[m];
    			}
    
    			start = strstr(tempaback,input);
    
    			if( start!=NULL)
    				{
    					end = start + strlen(input) - 1;
    
    					printf("starts at [%i,%i], ends at [%i,%i]\n",
    						i, ( ROWS - (start - tempaback)) , i , ( ROWS - (end-tempaback)));
    					return 0;
    					break;
    						
    				}
    				else
    					;
    			/*************************************************************************************/
    
    
    		}
    	}
    
    }
    
    int findDiag ( char wordsearch[][COLUMNS] , char input[] )
    {
    	int i;
    	int j;
    	int k;
    	char *start , *end;
    
    	char tempdiag[11];
    
    	for ( i = 0 ; i < ROWS ; i ++ )
    	{
    		for( j = 0 ; j < COLUMNS ; j++ )
    		{
    			if(wordsearch[i][j] == input[0] )
    			{
    				for( k =0 ; k < ROWS ; k++ )
    				{
    					while( input[k] == wordsearch[i+k][j+k] )
    					{
    
    						start = strstr(tempdiag,input);
    
    						
    							if(start != NULL)
    							{
    								end = start + strlen(input) - 1;
    								
    								printf("starts at [%i,%i], ends at [%i,%i]\n",
    									i,j,(i+(end-tempdiag)),(j+(end-tempdiag)));
    								return 0;
    								break;
    							}
    							else
    								;
    					}
    				}
    			}
    			else 
    				;
    		}
    	}	
    }
    To the extent of my knowledge, all but the diagonal work fine. If you can see something obvious wrong with it or some simple mistake , leave your two cents. Thanks!!!

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    nevermind! i think i figured it out. this is my diagonal function...
    Code:
    int findDiag ( char wordsearch[][COLUMNS] , char input[] )
    {
    	int row , column , k ;
    	char *start , *end;
    	char tempdiag[11];
    
    	for(row = 0 ; row < ROWS ; row++)
    	{
    		for(column = 0; column < COLUMNS ; column++ )
    		{
    			if(wordsearch[row][column] == input[0])
    			{
    				for( k = 0; k < ROWS ; k++ )
    				{
    					tempdiag[k] = wordsearch[row+k][column+k];
    
    					start = strstr(tempdiag,input);
    
    						if(start != NULL)
    						{
    							end = start + strlen(input) - 1;
    							
    							printf("starts at [%i,%i], ends at [%i,%i]\n",
    								row,column,(row + (end-tempdiag)),(column +(end-tempdiag)));
    							return 0;
    							break;
    						}
    						else
    							;
    				}
    			}
    
    			else
    				;
    				
    		}
    	}
    	printf("WORD NOT FOUND\n");
    	return 1;
    }
    seems to work fine! THANKS prelude for all your help!

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    I have one question left....
    if i were to read in a word list from a file that then has to be searched for in the puzzle... how would i do that?

    when i use fgets(buffer, n , file) to get a string , it puts the null character in the string, and it wont find the word if the null character is there... And I cant use the interger (n) to determine when to stop reading in characters because the words are all different sizes in the word. Also I need to accomodate any sized word list... How can i make sure it goes to the end of the file. Do i use feof() or something else. Thanks all.

  11. #11
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    is there any way i can read it in by character and store it into a string which can then be tested , instead of reading it in by a string? THANKS ALL.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    fgets() will leave a newline character in the array (normally) which can cause problems when doing string comparisons.

    To read by character, use fgetc(), as posted by vVv.

    From your earlier post:
    >>(fgets) puts the null character in the string, and it wont find the word if the null character is there
    If the string isn't NULL terminated, then how do you know where it ends. As I said, maybe you're having troubles with the newline character.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    is it possible that after you use fgets with the null or newline character, to trim the string , i was thinking something like this;
    Code:
    for ( i=0; /*some statement that is less than EOF*/ ; i++ )
    {
        fgets(buffer,n,wrdlist);
       while ( input[i] != '/0' || input[i] != '\n' )
    {
        input[i] = buffer[i];
    }
    /*then test here*/
    I havent compiled this by any means but would the idea of this work? thanks hammer and vVv and all others.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is it possible that after you use fgets with the null or newline character, to trim the string
    Yes, it's very common too:
    Code:
    #include <string.h>
    
    char s[20], *p;
    
    fgets ( s, sizeof s, stdin );
    
    if ( ( p = strchr ( s, '\n' ) ) != NULL )
      *p = '\0';
    -Prelude
    My best code is written with the delete key.

  15. #15
    Registered User
    Join Date
    Oct 2002
    Posts
    34
    Yay! Thanks a whole bunch Prelude! Thats much easier than what i was trying. Thank you everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings and Substrings using arrays
    By dcwang3 in forum C Programming
    Replies: 12
    Last Post: 02-18-2008, 06:28 AM
  2. A question concerning character arrays
    By ellipses in forum C Programming
    Replies: 3
    Last Post: 03-08-2005, 07:24 PM
  3. basic question to Arrays
    By doneirik in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2005, 01:57 AM
  4. question about strings
    By volk in forum C++ Programming
    Replies: 21
    Last Post: 05-06-2003, 04:08 PM
  5. stupid c question re arrays
    By C. Unger in forum C Programming
    Replies: 1
    Last Post: 04-10-2002, 08:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21