1

I am trying to take a char *, which represents a single word and i got the char * from a function, and put it into a 2d array but instead it just repeats the first letter of each word several times. the output looks like this

ttttttttttttttttttttttttttttttttvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvoooooooooooooooooooooooooo

and my input file is

three
versions
of
simple
with
spell
check
checking
program
we
will
write

I am not sure how to correctly transfer a char * into a 2d array here are all the functions i am using

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include "dict.h"



bool is_valid_entry( int strLength, char entry[] ){

    if( entry[0] != '"' && !isalpha(entry[0]) && entry[0] != '\''  ){
        return false;
    }

    strLength--; /* to allow for the zero index of the array*/

    for ( int i=1; i < strLength; i++ )
        if ( !isalpha( entry[i] ) ){

            return false;
        }

    if (!isalpha( entry[strLength] ) && !ispunct( entry[strLength] ) && entry[strLength] != '"' && entry[strLength] != '\'' ){

        return false;
    }
return true;/* if the entry passes all of the tests the function will return true meaning the the word is valid*/ 

}/* ends is_valid_entry( int strlength, char entry[] )*/



char * normalize( int strLength, char entry[], char output[] ){

    strLength--;/* to allow for the zero index of an array*/
    int j = 0;

    for( int i = 0;i < strLength; i++){/* converts all of the elements in the entry[] to lower case*/
        if (isupper( entry[i] ) ){
            entry[i] = tolower( entry[i] );
        }
    }

    if( ( entry[0] == '"' && entry[strLength] == '"' ) || (entry[0] == '\'' && entry[strLength] == '\'' ) ) {
        for(int i=1 ; i < strLength ; i++,j++ ){ 
            output[j] = entry[i];
        }
        output[j] = '\0';/* removes the last character which is either a '"' ir a '\''*/
        return output;/* returns the noramlized word*/

        }else if( entry[0] == '"' || entry[0] == '\'' ){
            for(int i = 1; j < strLength; i++, j++ ){/*i=1 in order to skip the first element in the entry arrary*/
                output[j] = entry[i];
            }
            output[j] = '\0';
            return output;/* returns the noramlized word*/

        } else if( entry[strLength] == '"' || ispunct( entry[strLength] ) || entry[strLength] == '\''){
                for(int i = 0;j < strLength; i++,j++ ){
                    output[j] = entry[i];
                }
                output[j] = '\0';
                return output;/* returns the noramlized word*/
            }


return entry;/* returns the original array since it does not need to be normalized*/

}/* ends normalize( int strlength, char entry[], char output[] )*/
    void load_dict(char *fileName, char dictionary[30000][31]) {

    FILE *fdict;
    fdict = fopen( fileName, "r" );/* file pointer for the dictionary file*/
    char *normDictWord;
    char normDict [33]; 

    int strLength,i,j,ch;    
    if ( fdict == NULL ){
        fprintf(stderr,"Could not open file: %s\n", fileName );/*checks to make sure the dictionary file can be opened*/
        exit(1);
    }

    for (i = 0; (ch = fgetc( fdict ) ) != EOF; i++ ) {          

        char word[33] = "";/* resets the array*/


        for (strLength = 0; !isspace( ch ) ; strLength++ ){
            word[strLength] = ch;
            ch = fgetc( fdict );            
                }

        if (is_valid_entry( strLength , word ) ){
            normDictWord = normalize( strLength , word , normDict );/*normalize then do the linear search then print the word if it is not found in the dictionary*/

            for(j = 0; j <= 31;j++){
                dictionary[i][j] = * normDictWord ;
                printf("%c",dictionary[i][j]);
            }

        }


    }
    fclose( fdict ); 
}
1
  • You mean you want to put char * in 1-D array right? Commented Sep 25, 2014 at 1:46

2 Answers 2

1

I cannot completely understand your code, fix formatting, and add some parts that are missing (otherwise I can't test it). However:

dictionary[i][j] = * normDictWord;

should be something like:

dictionary[i][j] = * (normDictWord + j);

or equivalently:

dictionary[i][j] = normDictWord[j] ;
Sign up to request clarification or add additional context in comments.

2 Comments

thank you but now it will only print one word. why does i not increase?
are you sure i does not incease? Try to print it.
1

Key problem (@pez)

  for (j = 0; j <= 31; j++) {
    // dictionary[i][j] = *normDictWord;
    dictionary[i][j] = normDictWord[j];
    printf("%c", dictionary[i][j]);
  }

Some other problems:

Original code will loop endlessly should the EOF condition occur before a space does. isspace(EOF) is false. This loop also does not prevent overrun.

char word[33] = "";

// for (strLength = 0; !isspace(ch); strLength++) {
for (strLength = 0; !isspace(ch) && ch != EOF && strLength < sizeof word; strLength++) {
  word[strLength] = ch;
  ch = fgetc(fdict);
}

The following does not "converts all of the elements in the entry[] to lower case", only ellements [0 ]... [original_string_length-2]. Maybe code should do strLength--; after the loop.

strLength--;/* to allow for the zero index of an array*/
...
for (int i = 0; i < strLength; i++) {/* */
  ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.