Hi Could somebody help me with my homework.

Use functions and structures.
Strings in input file 'AND', 'OR' ja 'NOT' must be replaced with symbols '&', 'v' ja '-' and saved into output file.

Example of input:
C = A AND B
D = A OR C
E = NOT (C OR D)
Output:
C = A & B
D = A v C
E = -(C v D)

#include <stdio.h>
#include <string.h>
#define max 100

int main()
{ 
static const char *a = "file.txt"; 
     fi = fopen( "*a", "r");
     fo = fopen("out.txt","w");
     char delims[] = " ";
     char *result = NULL;
     strtok( "*a", delims );
     while(*a!='\0')
     {
                    if(*a == 'AND')
                    *a='&';
                    else if(*a=='OR') 
                    *a='V';
                    else if(*a=='NOT')  
                    *a='-';
                    a++;
     }
     while( result != NULL ) {
         printf( "result is \%s\\n", result );
         result = strtok( NULL, delims );
     }
     fprintf(fo,"Input: %s \n",mass);
       fclose(fi);
       
replace(mass);
    printf("%s \n",mass);
    fprintf(fo,"Output: %s \n",array);
    fclose(fo);
    fclose(fi);
getch();
return 0;
}

I don't like that code for this assignment. There are no structs, and there are no functions. It might work, but it's not what you need.


how about having a prototype of a struct with 3 words:

#include <stdio.h>
#include <string.h>  //for strstr()
#define SIZE 100

typedef struct {
  char word1[]={"AND"};
  char word2[]={"OR"};
  char word3[]={"NOT"};
}words;

void check(char buffer[]);
 
int main(void) {
  //declare your variables for main, including a char buffer[SIZE]
  //use fgets(buffer, sizeof(buffer), fp) to get each row of text,
  //where fp is the FILE *fp for input, and the file has been opened in   
  //read mode.
  
  //call check(buffer); with each row of text
   //and make the changes to the buffer array
   //when it's OK, and returns from check, write buffer to the output file
   //using fp2, another file pointer, but for output.
  return 0;  
void check(char buffer) {
  //use strstr(), so you need a pointer to char: *pch
  //and you need one words struct like
  //words word, then use the dot operator: word.word1, word.word2, etc.
}

That should get you started in the right direction. Give that a try.

commented: Compile the code you posted.. -1
typedef struct {
  char word1[]={"AND"};
  char word2[]={"OR"};
  char word3[]={"NOT"};
}words;

What's the use of this?

Hi, I'm still trying. Could someone check. Not very sure about using struct and kontroll function.

#include <stdio.h>
#include <string.h>
#define SIZE 100

struct words{
  char *word1;
  char *word2;
  char *word3;
};
 
void kontroll(char massiiv, struct words) {
     if (strcmp(massiiv, s6nad.word1) !== 0)
     {
                                  s6nad.word1 = vasted.word1;
                                  return massiiv;
                                  }
     else if (strcmp(massiiv, s6nad.word2) !== 0)
     {
                                  s6nad.word2 = vasted.word2;
                                  return massiiv;
                                  }
     else if (strcmp(massiiv, s6nad.word2) !== 0)
     {
                                  s6nad.word1 = vasted.word1;
                                  return massiiv;
                                  }
} 
 
int main(void) {
    struct words s6nad;
    struct words vasted;
    s6nad.word1 = "AND";
    s6nad.word2 = "OR";
    s6nad.word3 = "NOT";
    vasted.word1 = "&";
    vasted.word2 = "v";
    vasted.word3 = "-";
    
    char massiiv[256];
    fi = fopen( "file.txt" , "r");
    fgets(massiiv, sizeof(massiiv), fi);
    fclose(fi);
    kontroll(massiiv);
    fo = fopen( "output.txt" , "w");
    fprintf(fo,"%s \n",massiiv);
    fclose(fo);
  return 0;
}

Not very sure about using struct and kontroll function.

struct is a lame attempt at making it look better. It doesn't compile anyway..

strtok won't work in this case. Use a search and replace instead:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define MY_NUM_STR_TO_REPLACE 3
#define MY_FILE_PROCESSOR_BUFFER_SIZE 256

char* get_transformed_line(const char* str) ;
char* strreplace(const char* src, const char* search, const char* replace);

const char* replacement_words[MY_NUM_STR_TO_REPLACE] = {"&", "v", "-"};
const char* words_2b_changed[MY_NUM_STR_TO_REPLACE] = {"AND", "OR", "NOT"};
char my_file_read_buffer[MY_FILE_PROCESSOR_BUFFER_SIZE];

int main() {
    const char* fileName = "file.txt";
    FILE* filePtr = fopen( fileName , "r");
    if ( NULL == filePtr ) {
        printf("\nCould not open the file %s. Err = %s", fileName, strerror(errno));
        return 1;
    }

    char* dummy = fgets(my_file_read_buffer, MY_FILE_PROCESSOR_BUFFER_SIZE, filePtr);
    while ( dummy != NULL ) {
        char* transformed = get_transformed_line(my_file_read_buffer);
        printf("\nold\n\"%s\"\nnew\n\"%s\"\n\n", my_file_read_buffer, transformed);
        dummy = fgets(my_file_read_buffer, MY_FILE_PROCESSOR_BUFFER_SIZE, filePtr);
    }

    fclose(filePtr);
    return 0;
}

// caller responsible for freeing returned string.
char* strreplace(const char* src, const char* search, const char* replace) {

    size_t numCharsInOrigString = strlen(src)* sizeof(char);
    // assumption: sizeof(search) >= sizeof(replace)
    // if not true, we'll run out of size of new_string.
    char* new_string = (char*) malloc(numCharsInOrigString);
    memset(new_string, 0, numCharsInOrigString);

    char* pos = strstr(src, search);
    size_t numChars = -1, currPosOldStr = 0, currPosNewStr = 0;
    while (pos != NULL) {
        numChars = pos - (src+currPosOldStr);
        // copy chars upto pos to new_string
        strncpy(new_string+currPosNewStr, src+currPosOldStr, numChars);
        currPosNewStr += numChars;
        currPosOldStr += numChars;
        // put the replacement string instead of original.
        strncpy(new_string+currPosNewStr, replace, strlen(replace));
        currPosNewStr += strlen(replace);
        currPosOldStr += strlen(search);
        pos = strstr(src+currPosOldStr, search);
    }
    // copy rest of the chars
    if ( currPosOldStr < strlen(src) - 1 )
        strncpy(new_string+currPosNewStr, src+currPosOldStr, strlen(src) - currPosOldStr );

    // terminate the string..
    new_string[currPosNewStr + strlen(src) - currPosOldStr] = '\0';

    //printf("strreplace()\n%s--%s--%s\n--%s--\n", src, search, replace, new_string);
    return new_string;
}

char* get_transformed_line(const char* str) {

    char* transformed = strdup(str);
    int i = 0;
    for ( ; i < MY_NUM_STR_TO_REPLACE; i++ ) {
        char* tmp = transformed;
        transformed = strreplace(transformed, words_2b_changed[i], replacement_words[i]);
        free(tmp);
    }

    return transformed;
}

The above code doesn't have any structs, which are required by the assignment. They don't have to be like I suggested, but the program must include them.

I agree with not using strtok(). You can check using words.word1, words.word2, and words.word3 with strstr(), for each line of text (in a loop, of course - and fgets() works very well like that). You might think of a better struct to use, also - but use one struct, at least.

The problem doesn't require struct. It's stupid of the prof to force students use it with this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.