Thread: Homework .Asap answer please.STRTOK = PROBLEM 2D ARRAY

  1. #16
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by christop View Post
    Please don't do this. The fgetc() and getchar() functions return an "int". "EOF" is some unspecified negative value, and some systems have an unsigned "char" type (C standards allow "char" to be either signed or unsigned), which means it will never be able to hold the value of "EOF".

    On systems where "char" is signed and can hold the value of "EOF", if a byte with that value happens to occur in a binary file you're reading, you'll stop reading the file prematurely. Imagine you're reading a file that has a byte with the value 0xFF. getchar() returns the value 0xFF (255), but if you cast it to a (signed) "char", that value gets converted to -1, which is the same value as EOF on many systems. The file doesn't actually end there, but your program mistakenly believes it does.

    As you can see, whether your system uses signed or unsigned char, you're going to have problems if you stuff the return value of fgetc() or getchar() into a char rather than an int.
    yeah I caught that in what I am doing here. didn't post it though. thanks for adding that.

    As long as you store the return value in an int and not a char, it is sufficient to check for EOF because it is guaranteed not to represent a valid character value.
    I neglected to show the change. It was up further in the whole of the code
    Code:
    //counts the words in the file
        // and how many letters used
        // skips noted puncuation marks
        int ch, letterCount = 0, count = 0;
        while((ch = fgetc(fp)) != EOF)
        {
            if(ch == ' ' || ch == '\n' )
            {
                
                count++;
                printf("ch= %c\ncount %d\n", ch, count);
            }
            else if (ch == ',' || ch == '.' || ch == '!' || ch == '?')
            ; // dont count it
            else
                letterCount += 1;
            
        }
        printf("letterCount %d\ncount %d\n", letterCount, count);
    Last edited by userxbw; 10-31-2017 at 11:51 AM.

  2. #17
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    Quote Originally Posted by userxbw View Post
    yeah I caught that in what I am doing here. didn't post it though. thanks for adding that.


    I neglected to show the change. It was up further in the whole of the code
    Code:
    //counts the words in the file
        // and how many letters used
        // skips noted puncuation marks
        int ch, letterCount = 0, count = 0;
        while((ch = fgetc(fp)) != EOF)
        {
            if(ch == ' ' || ch == '\n' )
            {
                
                count++;
                printf("ch= %c\ncount %d\n", ch, count);
            }
            else if (ch == ',' || ch == '.' || ch == '!' || ch == '?')
            ; // dont count it
            else
                letterCount += 1;
            
        }
        printf("letterCount %d\ncount %d\n", letterCount, count);
    Sorry for the late reply.First of all thanks for everything.
    at the programm: I want to ask the user to type how many letters does the word have and I will search the array with the words and any matchup I should store to another array for further usage in the programm.
    and What do you mean how the scanf cant read anything ,what should i use?
    Last edited by Isa-Elsino; 10-31-2017 at 01:26 PM.

  3. #18
    Banned
    Join Date
    Aug 2017
    Posts
    861
    how many letters does the word have? what word? do they know the word? if no then how will they know how many letters to type?
    or just type in a random number. then search for a word that matches that number amount then save it else where for later use,
    else print out a message back to them no words match that number?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
     
    #define WIDTH 50
     // retruns the letter count in prams
     int countStuffInFile(FILE *fp , int *lc)
     {
       int count = 0, letterCount = 0;
       int ch; //, ct = 0;
     //  int arr[35];
       
           while((ch = fgetc(fp)) != EOF)
        {
            if(ch == ' ' || ch == '\n' )
                count++;
            else if (ch == ',' || ch == '.' || ch == '!' || ch == '?')
                ; // dont count it
            else 
                letterCount += 1;
        }
     
            
        *lc = letterCount;
       return count;
    }
     
    int main(int argc, const char **argv)
    {
      
        FILE *fp;
        char *filename;
        char *cp;
        char line[255];
        
        filename = strdup(argv[1]);
        
        fp = fopen(filename, "r");
        if (fp == NULL) {
            fprintf(stderr, "File not found\n");
            exit(1);
        } 
        
        int lc, wordCount, i = 0;
         char twoDarray[40][40]; // 2d array to hold words
        
        // call to get word count,
        //                and letter count &lc gets that
        wordCount =  countStuffInFile(fp, &lc);  
      
       // open again to read it again 
       
        fp = fopen(filename, "r");
        if (fp == NULL) {
            fprintf(stderr, "File not found\n");
            exit(1);
        } 
        // reset i to zero
         i = 0;
        // keep it running to end of file
        while ( (fgets(line, sizeof(line), fp)) != NULL) {
            
            cp = strtok(line, " ");
            while ( cp != NULL)
            {            
                 int len = strlen(cp);
                // for loop
                // holds the array[i] in place
                // then fills the second elelmet outwards
                // to the right ->
                // array[i][d] = 'whatever lenght of word is'
                for ( int d = 0; d < len; d++)
                    { twoDarray[i][d] = cp[d]; } 
                 i++; //moves it to next first element 
                cp = strtok(NULL, " ");
             
            }
        }
      
        
        fclose(fp);
        // print out results
        for ( int h = 0; h < i; h++)
             printf("a: %s\n", twoDarray[h]);
            
           printf("words %d\nletter count %d\n",wordCount, lc);
    
    //***********************************************************************************
            int whatNum;
           printf("enter a number for match to word:");
           scanf("%d", &whatNum);
           
           for (int s = 0; s < sizeof(twoDarray)/sizeof(twoDarray[0]); s++)
           {
                int len = 0;
                len = strlen(twoDarray[s]);
            
                if( len == whatNum)
                     printf("your word is %s\n", twoDarray[s]);
           }
    //*********************************************************************************************************
      
      free(filename);
        return 0;
    }
    this stores complete words in a 2d array seperated by a space. It just needs to be modified to make it dynamic and what tokens to use to know what to remove from sentence.

    Then you'll have an array you can check using maybe strlen off the element holding the words. using that two for loop method may work.

    If ( word == len) its a match then stuff that word into a different
    2d array using the same method. adding your method of dynamically creating an array perhaps?

    output of code posted here
    Code:
    userx@slackwhere:~/bin
    $ ./strtoky test
    a: hello!
    a: How
    a: are
    a: you?
    
    a: I
    a: am
    a: fine.
    
    a: Who
    a: are
    a: YOU?
    
    words 10
    letter count 30
    Last edited by userxbw; 10-31-2017 at 01:53 PM.

  4. #19
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    So I did with whatever i managed to understand .Really frustrating

    Code:
    int main()
    {
        /**/
        char  **wordarr, **wordpc;
        int option=1, library, howmany, i,number, numwords=0,count=0;
    
    
    
    
        //open the file
        FILE *fp;
        fp = fopen("file.txt", "r");
        if (fp == NULL) {
            fprintf(stderr, "File not found\n");
            exit(1);
        }
        //counts the words in the file
        int ch, letterCount = 0;
        while((ch = fgetc(fp)) != EOF){
            if(ch == ' ' || ch == '\n'){
                count++;
            }
            else if(ch == ',' || ch == '.' || ch == '!' || ch == '?');
            else
                letterCount +=1;
        }
        printf("letterCount %d\ncount %d\n", letterCount, count);
    
    
        //making the array
        wordarr = (char**)malloc(sizeof(char*) * count);
        for(i = 0; i<count; i++){
            wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
        }
        //
        //putting the words into the array
    
    
        char buffer[10000];
        fp = fopen("file.txt", "r");
        char *tok;
        i=0;
        while( fgets(buffer, 10000, fp) != NULL){
            tok = strtok(buffer, " .,!");
    
    
            while (tok != NULL) {
                strcpy(wordarr[(i)++], tok);
                tok = strtok(NULL, " .,!");
            }
        }
        fclose(fp);
    
    
        int j=0;
        for (j=0; j<i; j++) {
            printf("[%s]\n", wordarr[j]);
        }
        ////////////////
    
    
        printf("\nWrite how many letters does the word have?\n");
        scanf("%d", &number);
        int len;
        //goes to the first array and searches for words with NUM characters
        //stores the words to an array
                //make a static so it can hold the position of where the word is so
                //I can copy it to the other array
                    int stat_position[20];
        for(i=0; i<count; i++){
            len=0;
            len = strlen(wordarr[i]);
            printf("%d\n", len);
            if( len == number){
                numwords++;
                stat_position[i]=i;
            }
        }
            //ALLOCATE 2DARRAY
    
    
            wordpc = (char**)malloc(sizeof(char*) * numwords);
            for(i = 0; i<numwords; i++){
                wordpc[i] = (char*)malloc(sizeof(char) * WIDTH);
            }
        //Copy words
            for(i=0; i<numwords; i++){
                    wordarr[stat_position[i]] = wordpc[i];
            }
    
    
    
    
    
    
    
    
        //Deallocate the memory
        for (i=0; i<count; i++) {
            free(wordarr[i]);
            free(wordpc[i]);
        }
        //free(wordarr);
        //free(wordpc);
       return 0;
    }
    I dont how the fuq u are helping the other people.(great courage).I dont know what will do with my exercise ,maybe i will post something later the night or tommorow afternoon but thanks for everything and I hope you have a great life

  5. #20
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Isa-Elsino View Post
    So I did with whatever i managed to understand .Really frustrating

    .....

    I dont how the fuq u are helping the other people.(great courage).I dont know what will do with my exercise ,maybe i will post something later the night or tomorrow afternoon but thanks for everything and I hope you have a great life
    this is why asking questions, what is this doing? that helps.

    pull out a piece of paper then make it into graph paper, lines going up and down, and across.

    are you not making a 2d array dynamically to hold the words you want minus the tokptr?

    to do this you have an array[size][size]
    Code:
    array[ roll ][ col ];
    where roll is the place holder for each separate word.
    then the col is the length of the word, which needs to be as long as
    each random word that needs to go into that 2d array
    Code:
              col1  col2  col3   col4     col5       col6 
    roll 1:  w      h      y     \0
    roll 2   h      e      l      l       o          \0
    it is just like making a list on a piece of paper. one word each line. that is your roll.
    the count each letter that is your col

    the 2d array has to have each element long enough to hold a word of unknown length.
    so this code : you already know the data types declared.

    Code:
    char twoDarray[40][40];
    
    // that says 2d array that is
    // 40 lines long, max length of word that can
    // be kept inside of each 'line' is 39 (letters) + \0
    
    // while loop keeps fgets reading to end of file
      while ( (fgets(line, sizeof(line), fp)) != NULL) {
            // cp gets each word in a sentance
          //because it only looks for a space
        
           i = 0;
          // i set to zero so it starts on the first line / roll
            cp = strtok(line, " ");
            while ( cp != NULL)
            {            
                 int len = strlen(cp);
                
      // find the length of that word just gotten
      // for use in the col space of array [ roll ]  [ col ]
                   
       // it will check each cp one at a time
      // get its length.
    
             // now put that array into a for loop;
             // this is to put that word in the [ col ] 
           // using its length so it can write it in 
             // one letter at a time. 
              for ( int d = 0; d < len; d++)
              { // in here the loop holds it at the roll
                 // represented by i 
                         
                  twoDarray [ i = 0 ] [ d = 0 ... len ] = cp[ d = 0 ... len ];
    
    
    // d moves the word held in cp into the col of the array
      // it is a char pointer, by its nature it is already an array. 
      // so just write it as so.  = cp[d]
      //
      // d is now counting 0 to len of cp.
      // when the len has been reached the
      // words is now writen within the lengt of the 
       // second element
     // twoDarray [ roll] [ col length of word in here ] = cp [ same length of word in here]; 
                      // that is controlled by d in the for loop.
                   }
                  // when it is finished writing that word, it leaves the for loop;
    
                 i++;
               // i now equals 1 
            //  i is incremented by one , moving the arrays [ roll ] element down by 1
               // gets next word
                cp = strtok(NULL, " ");
    
    // repeats until file has been completely read. 
             
            }
        }
    all of that probably just makes it more confusing. but use a peice of paper for your make believe 2d array.
    just make your program small enough to work with that 2d array loop put printf in it where you need them to see what it is doing, or debug it and watch what it is doing.
    Attached Images Attached Images Homework .Asap answer please.STRTOK = PROBLEM 2D ARRAY-two-dimensional-array-jpg 
    Last edited by userxbw; 10-31-2017 at 03:31 PM.

  6. #21
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Homework .Asap answer please.STRTOK = PROBLEM 2D ARRAY-two-more-dimensional-array-jpg

    length of ho = (2 + 1 for \0) = 3
    needs to go on line 1 of paper, using 3 cols of space.

    Code:
    char *someLongWord;
    someLongWord = "ho";
    int len = strlen(someLongWord);
    
    
    //make 2d array
    char 2dArray[ 30 ] [ 30 ] ;
    int roll = 1;
    int col = 0;
    
    
    for ( col = 0; col < len; col++)
         2dArray[roll] [ col ] = someLongWord[ col ] ;
    what is the only var that is adding to it? the col
    roll = 1 it stays at 1 until you
    make it equal something else.

    one word added to a 2d array roll 1;
    its col = 3 in length because of h o \0;

    that simple.

    from your malloc all your doing as far as I can see is making an 2D array that says this
    Code:
    2dArray [ roll = amount of words in file ] [ col =  50 ];
    int roll = 0
    int col = 0;
    
    for ( col = 0; col < len; col++)
         2dArray[roll] [ col ] = token[ col ] ;
    is basically what your dynamic array will look like in code

    you'll just need to keep an eye on your length but 50 is a good amount.
    Last edited by userxbw; 10-31-2017 at 04:01 PM.

  7. #22
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define WIDTH 50
    
    
    int main()
    {
        /**/
        char  **wordarr, **wordpc;
        int option=1, library, howmany, i,number, numwords=0,count=0;
    
    
    
    
        //open the file
        FILE *fp;
        fp = fopen("file.txt", "r");
        if (fp == NULL) {
            fprintf(stderr, "File not found\n");
            exit(1);
        }
        //counts the words in the file
        int ch, letterCount = 0;
        while((ch = fgetc(fp)) != EOF){
            if(ch == ' ' || ch == '\n'){
                count++;
            }
            else if(ch == ',' || ch == '.' || ch == '!' || ch == '?');
            else
                letterCount +=1;
        }
        printf("letterCount %d\ncount %d\n", letterCount, count);
    
    
        //making the array
        wordarr = (char**)malloc(sizeof(char*) * count);
        for(i = 0; i<count; i++){
            wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
        }
        //
        //putting the words into the array
    
    
        char buffer[10000];
        fp = fopen("file.txt", "r");
        char *tok;
        i=0;
        while( fgets(buffer, 10000, fp) != NULL){
            tok = strtok(buffer, " .,!");
    
    
            while (tok != NULL) {
                strcpy(wordarr[(i)++], tok);
                tok = strtok(NULL, " .,!");
            }
        }
        fclose(fp);
    
    
        int j=0;
        for (j=0; j<i; j++) {
            printf("[%s]\n", wordarr[j]);
        }
        ////////////////
    
    
        printf("\nWrite how many letters does the word have?\n");
        scanf("%d", &number);
        int len;
        //goes to the first array and searches for words with NUM characters
        //stores the words to an array
                //make a static so it can hold the position of where the word is so
                //I can copy it to the other array
                    int stat_position[20];
        for(i=0; i<count; i++){
            len=0;
            len = strlen(wordarr[i]);
            printf("%d\n", len);
            if( len == number){
                numwords++;
                stat_position[i]=i;
            }
        }
            //ALLOCATE 2DARRAY
    
    
            wordpc = (char**)malloc(sizeof(char*) * numwords);
            for(i = 0; i<numwords; i++){
                wordpc[i] = (char*)malloc(sizeof(char) * WIDTH);
            }
        //Copy words
        int s=0;
            for(s=0; s<numwords; s++){
                    strcpy(wordpc[s], wordarr[stat_position[s]]);
            }
        j=0;
        for (j=0; j<2; j++) {
            printf("[%s]\n", wordpc[j]);
        }
    
    
    
    
    
    
    
    
        //Deallocate the memory
        for (i=0; i<count; i++) {
            free(wordarr[i]);
            free(wordpc[i]);
        }
    return 0;
    }
    run this. You will see that when u print out the words , the last one it puts one additional character probably \0 and thats why the bracket goes to the other line .Also for some fuqing reason i cant copy the words from one array to the other
    Last edited by Isa-Elsino; 10-31-2017 at 03:54 PM.

  8. #23
    Banned
    Join Date
    Aug 2017
    Posts
    861
    post your test file, I'm getting tired of chaing it to
    Code:
     main (int argc, char **argv)
    {
    then everything else to get it to open a file
    off command line.
    too it will help to see what you are working with as a whole

    this is first run,
    Code:
    userx@slackwhere:~/bin
    $ ./student_toky_2
    letterCount 30
    count 10
    Segmentation fault
    now I really got a look at it.

  9. #24
    Banned
    Join Date
    Aug 2017
    Posts
    861
    try this now and see if that workd for you.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
     
    #define WIDTH 50
     
     
    int main()
    {
        /**/
        char  **wordarr, **wordpc;
        int option=1, library, howmany, i,number, numwords=0,count=0;
     
         //open the file
        FILE *fp;
        fp = fopen("test", "r");
        if (fp == NULL) {
            fprintf(stderr, "File not found\n");
            exit(1);
        }
        //counts the words in the file
        int ch, letterCount = 0;
        while((ch = fgetc(fp)) != EOF){
            if(ch == ' ' || ch == '\n'){
                count++;
            }
            else if(ch == ',' || ch == '.' || ch == '!' || ch == '?');
            else
                letterCount +=1;
        }
        printf("letterCount %d\ncount %d\n", letterCount, count);
     
    // INCRESED COUNT BY 1 FOR SAFE KEEPING.
    // IT WAS CAUSING A SEG FAULT BY NOT ADDING ONE TO IT
        //making the array
        wordarr = (char**)malloc(sizeof(char*) * count+1);
        for(i = 0; i<count+1; i++){
            wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
        }
        //
        //putting the words into the array
     
      
        char buffer[10000];
        fp = fopen("test", "r");
        char *tok;
        i=0;
         
       // char TwoD[30][30];
        while( fgets(buffer, 10000, fp) != NULL){
            tok = strtok(buffer, " .,!");
            
          while (tok != NULL) {
              
                int len = strlen(tok);
                for ( int d = 0; d < len; d++)
                { // CHAGED IT TO MAKE IT COPY PROPERLY INTO YOUR
                    // 2D ARRAY 
                   wordarr[i][d] = tok[d];
               // strcpy(wordarr[i][ , tok);
               printf("HERE d %d   i %d\n",d,i);
                }
                i++;
                tok = strtok(NULL, " .,!");
            }
           
        }
        fclose(fp);
         
        
     
        int j=0;
        for (j=0; j<i; j++) {
            printf("[%s]\n", wordarr[j]);
        }
        ////////////////
     
     
        printf("\nWrite how many letters does the word have?\n");
        scanf("%d", &number);
        int len;
        //goes to the first array and searches for words with NUM characters
        //stores the words to an array
                //make a static so it can hold the position of where the word is so
                //I can copy it to the other array
              int stat_position[20];
                    
        for(i=0; i<count; i++){
            len=0;
            len = strlen(wordarr[i]);
            printf("%d\n", len);
            if( len == number){
                numwords++;
                stat_position[i]=i;
            }
        }
            //ALLOCATE 2DARRAY
     
            wordpc = (char**)malloc(sizeof(char*) * numwords);
            for(i = 0; i<numwords; i++){
                wordpc[i] = (char*)malloc(sizeof(char) * WIDTH);
            }
        //Copy words
        int s=0;
            for(s=0; s<numwords; s++){
                    strcpy(wordpc[s], wordarr[stat_position[s]]);
            }
        j=0;
    // CHANGED IT TO GET THE ARRAY LENGTH/SIZE
        for (j=0; j<sizeof(wordpc)/sizeof(wordpc[0]); j++) {
            printf("[%s]\n", wordpc[j]);
        }
        //Deallocate the memory
        for (i=0; i<count; i++) {
            free(wordarr[i]);
        }
    
    return 0;
    }
    results
    Code:
    [hello]
    [How]
    [are]
    [you?
    ]
    [I]
    [am]
    [fine]
    [
    ]
    [Who]
    [are]
    [YOU?
    ]
    
    Write how many letters does the word have?
    4
    5
    3
    3
    5
    1
    2
    4
    1
    3
    3
    [hello]
    STILL WORKING ON THAT BRACKET THING.
    Last edited by userxbw; 10-31-2017 at 04:39 PM.

  10. #25
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define WIDTH 50
    
    
    int main()
    {
        /**/
        char  **wordarr, **wordpc;
        int option=1, library, howmany, i,number, numwords=0,count=0;
    
    
    
    
        //open the file
        FILE *fp;
        fp = fopen("file.txt", "r");
        if (fp == NULL) {
            fprintf(stderr, "File not found\n");
            exit(1);
        }
        //counts the words in the file
        int ch, letterCount = 0;
        while((ch = fgetc(fp)) != EOF){
            if(ch == ' ' || ch == '\n'){
                count++;
            }
            else if(ch == ',' || ch == '.' || ch == '!' || ch == '?');
            else
                letterCount +=1;
        }
        printf("letterCount %d\ncount %d\n", letterCount, count);
    
    
        //making the array
        wordarr = (char**)malloc(sizeof(char*) * count+1);
        for(i = 0; i<count; i++){
            wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
        }
        //
        //putting the words into the array
    
    
        char buffer[10000];
        fp = fopen("file.txt", "r");
        char *tok;
        i=0;
        int d=0;
    
    
        while( fgets(buffer, 10000, fp) != NULL){
            tok = strtok(buffer, " .,!");
            while (tok != NULL) {
                int len = strlen(tok);//
                for(d=0; d<len; d++){
                    wordarr[i][d] = tok[d];
                }
                //strcpy(wordarr[(i)++], tok);
                i++;
                tok = strtok(NULL, " .,!");
            }
        }
        fclose(fp);
    
    
        int j=0;
        for (j=0; j<i; j++) {
            printf("[%s]\n", wordarr[j]);
        }
        ////////////////
    
    
        printf("\nWrite how many letters does the word have?\n");
        scanf("%d", &number);
        int len;
        //goes to the first array and searches for words with NUM characters
        //stores the words to an array
                //make a static so it can hold the position of where the word is so
                //I can copy it to the other array
                    int stat_position[20];
        for(i=0; i<count; i++){
            len=0;
            len = strlen(wordarr[i]);
            printf("%d\n", len);
            if( len == number){
                numwords++;
                stat_position[i]=i;
            }
        }
            //ALLOCATE 2DARRAY
    
    
            wordpc = (char**)malloc(sizeof(char*) * numwords);
            for(i = 0; i<numwords; i++){
                wordpc[i] = (char*)malloc(sizeof(char) * WIDTH);
            }
        //Copy words
            int s=0;
            for(s=0; s<numwords; s++){
                    strcpy(wordpc[s], wordarr[stat_position[s]]);
            }
            j=0;
            for (j=0; j<sizeof(wordpc)/sizeof(wordpc[0]); j++) {
                printf("[%s]\n", wordpc[j]);
            }
    
    
        //Deallocate the memory
        for (i=0; i<count; i++) {
            free(wordarr[i]);
            free(wordpc[i]);
        }
    
    
    
        return 0;
    }
    results
    Code:
    letterCount 26
    count 4
    [climate]
    [coal(!]
    [ecosystem]
    [energy
    ]
    
    
    Write how many letters does the word have?
    4
    7
    7
    9
    7
    [└]
    
    
    Process returned 0 (0x0)   execution time : 3.724 s
    Press any key to continue.

  11. #26
    Banned
    Join Date
    Aug 2017
    Posts
    861
    got it!
    Code:
     while( fgets(buffer, 10000, fp) != NULL){
    
            // fixes that print [ out ]
            int len = strlen(buffer);
            
             if (len > 0 && buffer[len-1] == '\n') {
                buffer[--len] = '\0';
            }
            tok = strtok(buffer, " .,!");
            
          while (tok != NULL) {
              
                int len = strlen(tok);
                for ( int d = 0; d < len; d++)
                { // CHAGED IT TO MAKE IT COPY PROPERLY INTO YOUR
                    // 2D ARRAY 
                   wordarr[i][d] = tok[d];
                }
                i++;
                tok = strtok(NULL, " .,!");
            }
           
        }
        fclose(fp);
    add that red to your while loop where it is at in your code.

  12. #27
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Isa-Elsino View Post

    Code:
    letterCount 26
    count 4
    [climate]
    [coal(!]
    [ecosystem]
    [energy
    ]
    Write how many letters does the word have?
    4
    7
    7
    9
    7
    [└]
    
    
    Process returned 0 (0x0)   execution time : 3.724 s
    Press any key to continue.
    you do not show any 4 letter words...

    and why you pickin' 4 letter words? what did you mom say about that? hehe

  13. #28
    Banned
    Join Date
    Aug 2017
    Posts
    861
    all of this I do not believe it is really working properly
    Code:
     
        printf("\nWrite how many letters does the word have?\n");
        scanf("%d", &number);
        int len;
        //goes to the first array and searches for words with NUM characters
        //stores the words to an array
                //make a static so it can hold the position of where the word is so
                //I can copy it to the other array
              int stat_position[20];
         printf("count %d sizeof %lu\n", count, sizeof(wordarr)/sizeof(wordarr[0][0]) );
        for(i=0; i<count; i++){
            len=0;
            len = strlen(wordarr[i]);
         //   printf("%d\n", len);
            if( len == number){
                numwords++;
                stat_position[i]=i;
            }
        }
            //ALLOCATE 2DARRAY
     
            wordpc = (char**)malloc(sizeof(char*) * numwords);
            for(i = 0; i<numwords; i++){
                wordpc[i] = (char*)malloc(sizeof(char) * WIDTH);
            }
        //Copy words
        int s=0;
            for(s=0; s<numwords; s++){
                    strcpy(wordpc[s], wordarr[stat_position[s]]);
            }
        j=0;
        
        printf("count %d sizeof %lu\n", count, sizeof(wordpc)/sizeof(wordpc[0]) );
        
        for (j=0; j<sizeof(wordpc)/sizeof(wordpc[0]); j++) {
            // if lenght matches the number requested
            // print it out to screen
        //    int len = strlen(wordpc[j]);
        //    if ( len == number)
                printf("[fg %s]\n", wordpc[j]);
        }
        for (j=0; j<sizeof(wordpc)/sizeof(wordpc[0]); j++) {
            // if lenght matches the number requested
            // print it out to screen
            int len = strlen(wordpc[j]);
            if ( len == number)
                printf("[%s]\n", wordpc[j]);
        }
        //Deallocate the memory
        for (i=0; i<count; i++) {
            free(wordarr[i]);
          //  free(wordpc[i]);
        }
    free(wordpc[i]);
    take a look at how your adding words to your 2d array wordarr.
    then take a look at how you're doing it with your 2d wordpc (but treating it differently )
    Last edited by userxbw; 10-31-2017 at 05:39 PM.

  14. #29
    Banned
    Join Date
    Aug 2017
    Posts
    861
    updated, done for now? I fixed your int array loop .. still needs a little more work. I left in the printf's so you can see what is going on.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
     
    #define WIDTH 50
     
     
    int main()
    {
        /**/
        char  **wordarr, **wordpc;
        int option=1, library, howmany, i,number, numwords=0,count=0;
     
         //open the file
        FILE *fp;
        fp = fopen("test", "r");
        if (fp == NULL) {
            fprintf(stderr, "File not found\n");
            exit(1);
        }
        //counts the words in the file
        int ch, letterCount = 0;
        while((ch = fgetc(fp)) != EOF){
            if(ch == ' ' || ch == '\n'){
                count++;
            }
            else if(ch == ',' || ch == '.' || ch == '!' || ch == '?');
            else
                letterCount +=1;
        }
        printf("letterCount %d\ncount %d\n", letterCount, count);
     
    // INCRESED COUNT BY 1 FOR SAFE KEEPING.
    // IT WAS CAUSING A SEG FAULT BY NOT ADDING ONE TO IT
        //making the array
        wordarr = (char**)malloc(sizeof(char*) * count+1);
        for(i = 0; i<count+1; i++){
            wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
        }
        //
        //putting the words into the array
     
      
        char buffer[10000];
        fp = fopen("test", "r");
        char *tok;
        i=0;
         
       // char TwoD[30][30];
        while( fgets(buffer, 10000, fp) != NULL){
            
            int len = strlen(buffer);
            
             if (len > 0 && buffer[len-1] == '\n') {
                buffer[--len] = '\0';
            }
            tok = strtok(buffer, " .,!");
            
          while (tok != NULL) {
              
                int len = strlen(tok);
                for ( int d = 0; d < len; d++)
                { // CHAGED IT TO MAKE IT COPY PROPERLY INTO YOUR
                    // 2D ARRAY 
                   wordarr[i][d] = tok[d];
                }
                i++;
                tok = strtok(NULL, " .,!");
            }
           
        }
        fclose(fp);
         
        
     
        int j=0;
        for (j=0; j<i; j++) {
            printf("[%s]\n", wordarr[j]);
        }
         
        ////////////////
     
     
        printf("\nWrite how many letters does the word have?\n");
        scanf("%d", &number);
        int len;
        //goes to the first array and searches for words with NUM characters
        //stores the words to an array
                //make a static so it can hold the position of where the word is so
                //I can copy it to the other array
        int stat_position[20];
         int move1 = 0;
        // printf("count %d sizeof %lu\n", count, sizeof(wordarr)/sizeof(wordarr[0][0]) );
        for(i=0; i<count; i++)
        {
            len=0;
            len = strlen(wordarr[i]);
        //    printf("i = %d\n",i);
            
            if( len == number)
            {
            //     printf("len %d i= %d\n", len, i);
                numwords++;
                //only move that int array element by one if len found
                // to keep what element number the wordarr is inside of it
               // and not put the proper number element of your wordarr 
           // in the same int element number 
     
                stat_position[move1]=i;
               
              printf("move1 %d word is %s\n", move1,  wordarr[i]);
    
               move1++;
            }
        }
            //ALLOCATE 2DARRAY
     
            wordpc = (char**)malloc(sizeof(char*) * numwords);
            for(i = 0; i<numwords; i++){
                wordpc[i] = (char*)malloc(sizeof(char) * WIDTH);
            }
    printf("\n");
        for (int h = 0; h < numwords ; h++)
                printf("%d\n", stat_position[h]);
                
        printf("\n");
                
     
        
                // now you have your int array with the proper
            // amount of elelments holding the peoper number
            // of which element number wordarr has the
            // chosen words in it by length
            
            
        for (int d = 0; d < move1; d++)
            strcpy(wordpc[d], wordarr[stat_position[d]]);
    
    
    
        j=0;
        
        printf("count %d sizeof %lu\n", count, sizeof(wordpc)/sizeof(wordpc[0]) );
        
        for (j=0; j<numwords; j++) {
            // if lenght matches the number requested
            // print it out to screen
        //    int len = strlen(wordpc[j]);
        //    if ( len == number)
                printf("[fg %s]\n", wordpc[j]);
        }
        for (j=0; j<sizeof(wordpc)/sizeof(wordpc[0]); j++) {
            // if lenght matches the number requested
            // print it out to screen
            int len = strlen(wordpc[j]);
            if ( len == number)
                printf("[%s]\n", wordpc[j]);
        }
        //Deallocate the memory
        for (i=0; i<count; i++) {
            free(wordarr[i]);
          //  free(wordpc[i]);
        }
    free(wordpc); // you need use / get the proper count for its size  to free it if you want to do that in a loop
    return 0;
    }
    Last edited by userxbw; 10-31-2017 at 07:38 PM.

  15. #30
    Registered User
    Join Date
    Oct 2017
    Posts
    27
    Quote Originally Posted by userxbw View Post
    updated, done for now? I fixed your int array loop .. still needs a little more work. I left in the printf's so you can see what is going on.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
     
    #define WIDTH 50
     
     
    int main()
    {
        /**/
        char  **wordarr, **wordpc;
        int option=1, library, howmany, i,number, numwords=0,count=0;
     
         //open the file
        FILE *fp;
        fp = fopen("test", "r");
        if (fp == NULL) {
            fprintf(stderr, "File not found\n");
            exit(1);
        }
        //counts the words in the file
        int ch, letterCount = 0;
        while((ch = fgetc(fp)) != EOF){
            if(ch == ' ' || ch == '\n'){
                count++;
            }
            else if(ch == ',' || ch == '.' || ch == '!' || ch == '?');
            else
                letterCount +=1;
        }
        printf("letterCount %d\ncount %d\n", letterCount, count);
     
    // INCRESED COUNT BY 1 FOR SAFE KEEPING.
    // IT WAS CAUSING A SEG FAULT BY NOT ADDING ONE TO IT
        //making the array
        wordarr = (char**)malloc(sizeof(char*) * count+1);
        for(i = 0; i<count+1; i++){
            wordarr[i] = (char*)malloc(sizeof(char) * WIDTH);
        }
        //
        //putting the words into the array
     
      
        char buffer[10000];
        fp = fopen("test", "r");
        char *tok;
        i=0;
         
       // char TwoD[30][30];
        while( fgets(buffer, 10000, fp) != NULL){
            
            int len = strlen(buffer);
            
             if (len > 0 && buffer[len-1] == '\n') {
                buffer[--len] = '\0';
            }
            tok = strtok(buffer, " .,!");
            
          while (tok != NULL) {
              
                int len = strlen(tok);
                for ( int d = 0; d < len; d++)
                { // CHAGED IT TO MAKE IT COPY PROPERLY INTO YOUR
                    // 2D ARRAY 
                   wordarr[i][d] = tok[d];
                }
                i++;
                tok = strtok(NULL, " .,!");
            }
           
        }
        fclose(fp);
         
        
     
        int j=0;
        for (j=0; j<i; j++) {
            printf("[%s]\n", wordarr[j]);
        }
         
        ////////////////
     
     
        printf("\nWrite how many letters does the word have?\n");
        scanf("%d", &number);
        int len;
        //goes to the first array and searches for words with NUM characters
        //stores the words to an array
                //make a static so it can hold the position of where the word is so
                //I can copy it to the other array
        int stat_position[20];
         int move1 = 0;
        // printf("count %d sizeof %lu\n", count, sizeof(wordarr)/sizeof(wordarr[0][0]) );
        for(i=0; i<count; i++)
        {
            len=0;
            len = strlen(wordarr[i]);
        //    printf("i = %d\n",i);
            
            if( len == number)
            {
            //     printf("len %d i= %d\n", len, i);
                numwords++;
                //only move that int array element by one if len found
                // to keep what element number the wordarr is inside of it
               // and not put the proper number element of your wordarr 
           // in the same int element number 
     
                stat_position[move1]=i;
               
              printf("move1 %d word is %s\n", move1,  wordarr[i]);
    
               move1++;
            }
        }
            //ALLOCATE 2DARRAY
     
            wordpc = (char**)malloc(sizeof(char*) * numwords);
            for(i = 0; i<numwords; i++){
                wordpc[i] = (char*)malloc(sizeof(char) * WIDTH);
            }
    printf("\n");
        for (int h = 0; h < numwords ; h++)
                printf("%d\n", stat_position[h]);
                
        printf("\n");
                
     
        
                // now you have your int array with the proper
            // amount of elelments holding the peoper number
            // of which element number wordarr has the
            // chosen words in it by length
            
            
        for (int d = 0; d < move1; d++)
            strcpy(wordpc[d], wordarr[stat_position[d]]);
    
    
    
        j=0;
        
        printf("count %d sizeof %lu\n", count, sizeof(wordpc)/sizeof(wordpc[0]) );
        
        for (j=0; j<numwords; j++) {
            // if lenght matches the number requested
            // print it out to screen
        //    int len = strlen(wordpc[j]);
        //    if ( len == number)
                printf("[fg %s]\n", wordpc[j]);
        }
        for (j=0; j<sizeof(wordpc)/sizeof(wordpc[0]); j++) {
            // if lenght matches the number requested
            // print it out to screen
            int len = strlen(wordpc[j]);
            if ( len == number)
                printf("[%s]\n", wordpc[j]);
        }
        //Deallocate the memory
        for (i=0; i<count; i++) {
            free(wordarr[i]);
          //  free(wordpc[i]);
        }
    free(wordpc); // you need use / get the proper count for its size  to free it if you want to do that in a loop
    return 0;
    }
    Hey man thanks for ecerything, but I have a question.Do you know how can i sort 2 different data types of array so i dont lose the connections.Frequency of the characters and the characters.Do I need to use Struct??

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with homework on array(2)
    By mr_plopper in forum C Programming
    Replies: 8
    Last Post: 10-14-2015, 10:28 AM
  2. problem with homework on array
    By Kenny Dearing in forum C Programming
    Replies: 4
    Last Post: 10-12-2015, 12:04 PM
  3. Help with comparing answer to math problem and user's answer
    By Jake Garbelotti in forum C Programming
    Replies: 6
    Last Post: 07-20-2012, 10:12 PM
  4. need homework help asap with if statement
    By automagp68 in forum C Programming
    Replies: 26
    Last Post: 01-27-2012, 02:19 PM

Tags for this Thread