can u solve it?

This is a discussion on can u solve it? within the C Programming forums, part of the General Programming Boards category; can anyone tell me why this programme isnt working? its meant to count how many words that you enter appear ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    32

    can u solve it?

    can anyone tell me why this programme isnt working? its meant to count how many words that you enter appear in the second sentence. run it and see wot i mean. Thanks,




    PROGRAMME:

    #include <stdio.h>

    int main() {
    FILE *file1;
    FILE *file2;
    FILE *file3;

    char sentence1[50];
    char sentence2[50];
    char sentence3[50];


    int i, j, k, c;
    int word = 0;

    file1 = fopen("sentence1.txt", "w+"); /* we create a file for reading and writing */
    file2 = fopen("sentence2.txt", "w+"); /* we create a file for reading and writing */
    file3 = fopen("sentence3.txt", "w+"); /* we create a file for reading and writing */

    printf("****************************************** ***********\n");
    printf("*A PROGRAM TO STORE TEXT IN 3 FILES THEN RECALL THE *\n");
    printf("*SECOND AND COUNT THE NUMBER OF TIMES A WROD APPEARS*\n");
    printf("****************************************** ***********\n\n");

    if(file1==NULL) {
    printf("Error: can't create file.\n");
    return 1;
    }
    else {


    printf("Enter you first sentence (less than 50 characters):\n");
    gets(sentence1);

    printf("File created successfully.\n");


    for(i=0 ; sentence1[i] ; i++) {
    fputc(sentence1[i], file1);
    }

    rewind(file1); /* reset the file pointer's position */

    printf("Contents of the file: ");

    while(!feof(file1)) {
    printf("%c", fgetc(file1));
    }

    printf("\n");
    fclose(file1);


    }

    if(file2==NULL) {
    printf("Error: can't create file.\n");
    return 1;
    }
    else {

    printf("Enter your second sentence (less than 50 characters):\n");
    gets(sentence2);

    printf("File created successfully.\n");


    for(j=0 ; sentence2[j] ; j++) {
    fputc(sentence2[j], file2);
    }

    rewind(file2); /* reset the file pointer's position */

    printf("Contents of the file: ");

    while(!feof(file2)) {
    printf("%c", fgetc(file2));
    }

    printf("\n");
    fclose(file2);


    }
    if(file3==NULL) {
    printf("Error: can't create file.\n");
    return 1;
    }
    else {


    printf("Enter your third sentence (less than 50 characters):\n");
    gets(sentence3);

    printf("File created successfully.\n");

    for(k=0 ; sentence3[k] ; k++) {
    fputc(sentence3[k], file3);
    }

    rewind(file3); /* reset the file pointer's position */

    printf("Contents of the file: ");

    while(!feof(file3)) {
    printf("%c", fgetc(file3));
    }

    printf("\n");
    fclose(file3);

    }


    file2 = fopen("sentence2.txt", "r"); /* open a text file for reading */

    if(file2==NULL) {
    printf("Error: can't open file.\n");
    /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
    return 1;
    }
    else {
    printf("File opened successfully. Contents:\n\n");
    printf("The text you entered from line 2 is:\n");
    while(1) { /* keep looping... */
    c = fgetc(file2);
    if(c!=EOF) {


    printf("%c", c); /* print the file one character at a time */
    }
    else {
    break; /* ...break when EOF is reached */
    }


    }
    printf("\n\nOk, please enter a word to look for in this sentence.\n");

    scanf("%d", &word);

    if (c == word)
    word++;
    }
    printf("This text appears word %d times.\n", word);


    fclose(file2);
    return 0;


    }
    Last edited by brad123; 04-29-2002 at 08:43 AM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    scanf("%d", &word);
    if (c == word)
    Whats going on here? You ask for a word, and store the input in an int? Presumably you want a string (char array).

    Also, when you do string comparisons, you'll need to use strcmp.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    baically, i need to know how to count how many times an inputed word appears in sentence 2. any ideas?

    Brad.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Take the line and break it up into tokens, test those tokens against the keyword and if they match, increment a counter. Something like this:
    Code:
    tok = strtok ( copy_of_line, " " );
    while ( tok != NULL ) {
      if ( strcmp ( tok, word ) == 0 )
        count++;
      tok = strtok ( NULL, " " );
    }
    -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
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char sentence[] = "this is is is is a sentence";
    	char *delims = { " .," };
    	char *ptr;
    	int count = 0;
    	
    	ptr = strtok(sentence, delims);
    	
    	while (ptr != NULL)
    	{
    		if (strcmp(ptr, "is") == 0)
    			count++;
    		ptr = strtok(NULL, delims);
    	}
    	
    	printf("Found word >is< %d times\n", count);
    }
    Change this to suit.

    [edit] Doh! Beaten to it! [/edit]
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >ptr = strtok(sentence, delims);
    This is unsafe if you need to use sentence elsewhere. strtok modifies the array you give it, so it would be best to make a copy and then use strtok on the copy.

    And you forgot to return something from main.

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

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    32
    thanks again prelude, but ive never used this function strtok before and although uve shown me wot to do could i trouble you again to help me integrate it into my program? its the final piece in the puzzle.

    Brad

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    Also, perhaps you should think about encapsulating the code into more modular functions (yours is a mess!!)...

    Anyway, this handy little function utilizes the "string.h" library function "strstr". Basically how "strstr" works is if it finds a match, it returns a pointer to the first char in that string where the match begins. Now so that it doesn't keep returning the same pointer, you increment it and search again, using the returned pointer...
    If you uncomment the parts I hid from the compiler, you can see how it internally works...

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

    int Count( char *string, char *find )
    {
    int count = 0;

    char *s = strstr(string, find);

    /*
    if(s!=NULL) {
    printf("Current Position: %s", s);
    getch();
    }
    */

    while(s != NULL)
    {
    count++;
    s++;
    s = strstr(s, find);

    /*
    if(s != NULL)
    {
    printf("Current Position: %s", s);
    getch();
    }
    */
    }
    /*
    printf("Counted: %i", count);
    getch();
    */
    return count;
    }






    int main()
    {


    char a[] = "so so so so";
    char b[] = "so";

    int i = Count(a, b);

    printf("Count: %i", i);
    getch();



    return 0;
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Prelude
    >ptr = strtok(sentence, delims);
    This is unsafe if you need to use sentence elsewhere. strtok modifies the array you give it, so it would be best to make a copy and then use strtok on the copy.

    And you forgot to return something from main.

    Twas only a demo prog, honest guv!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >could i trouble you again to help me integrate it into my program?
    Sure, you try to get it working and if you have any problems that you can't solve, I'll help out. Seriously, you learn more if you try to figure it out for yourself instead of asking for help right away. As far as I can tell, all you have to do is read the second sentence from the file, plug in the code that either I or Hammer gave you and change the names accordingly.

    >Twas only a demo prog, honest guv!
    Be sure to mention that, or someone may mistake it for bulletproof pro code. I try to keep my code posted here as well written as at work, if only to promote good style.

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

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    Sorry, forgot to use code tags...

    Here's the uncommented version:


    Code:
    
    int Count( char *string, char *find ) 
    { 
      int count = 0; 
    
      char *s = strstr(string, find); 
    
      while(s != NULL) 
       { 
        count++; 
         s++; 
         s = strstr(s, find);
       } 
    
    return count; 
    }
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Prelude

    Be sure to mention that, or someone may mistake it for bulletproof pro code. I try to keep my code posted here as well written as at work, if only to promote good style.
    Understood ! Will do in future.
    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
    Apr 2002
    Posts
    32
    ok , ive put this piece of code in:

    if (strcmp(ptr, "is") == 0)

    from hammer, all i need to do now is make the search word not a constant ("is"),

    if i use the line

    scanf("%c", &word);

    how do i put the character variable 'word' into this top line of code? ive tried it and it says "must be of type <ptr>const char not char".
    Last edited by brad123; 04-29-2002 at 11:42 AM.

  14. #14
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,700
    try:

    char word[100];

    scanf("%s", word);

    The compiler was trying to tell you that you attempted to scan a string ( char array ) into a single char!
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    %c is a single char, you need %s for a string. Make sure the array is big enough first.

    gotto run.... answer ya more later if needed.
    Last edited by Hammer; 04-29-2002 at 11:44 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solve efficiently
    By jack_carver in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 06:56 AM
  2. Riddles to solve
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-11-2006, 04:07 PM
  3. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  4. Help to solve this problem
    By Romashka in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2002, 09:32 AM

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