Like Tree3Likes
  • 2 Post By Salem
  • 1 Post By Adak

I can process a string input, but not get a result for every sentence in the string.

This is a discussion on I can process a string input, but not get a result for every sentence in the string. within the C Programming forums, part of the General Programming Boards category; Here is the code: Code: #include <stdio.h> #include <string.h> #include <stdlib.h> /* function prototype to count the number of characters ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    229

    I can process a string input, but not get a result for every sentence in the string.

    Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    /* function prototype to count the number of characters in a string */
    int countchar (char []);
    
    /* function prototype to find the first and last letter in the word */
     
    char find_letter (char* a, char* b);
    char red;
     
    /* function prototype to reverse the letters in the word */
     
    char* rev(char* str);
    char* reverse;
     
    /* function so I don't have to type getch all over the place */
     
    void MyExit(void) { system("pause"); }
     
    /* the main program */
     
    int main ()
    {
        /* declaring and initiaizing variables */
    	FILE *sp;
    	FILE *fp;
    	char *filedata = malloc(300);
      char str2[7];
      char * pch = malloc(300);
      char vowels[] = "aeiouy";
      char letters[] = "bcdfghjklmnpqrstvwxz";
      char *b = malloc(sizeof(char)*1000);
    
      float one, *A;
      float two, *B;
      float three, *C;
      float four, *D;
     float total, *E;
    
     float five, *F;
      float six, *G;
      float seven, *H;
      float eight, *I;
     float total_2, *J;
     float percentage_1, *K;
     float percentage_2, *L;
     float percentage_3, *M;
     float percentage_4, *N;
     int numchar;
    
      A = &one;
      B = &two;
      C = &three;
      D = &four;
      E = &total;
    
      *A = 0;
      *B = 0;
      *C = 0;
      *D = 0;
      *E = 0;
    
      F = &five;
      G = &six;
      H = &seven;
      I = &eight;
      J = &total_2;
      K = &percentage_1;
      L = &percentage_2;
      M = &percentage_3;
      N = &percentage_4;
      *F = 0;
      *G = 0;
      *H = 0;
      *I = 0;
      *J = 0;
      *K = 0;
      *L = 0;
      *M = 0;
      *N = 0;
    
      atexit(MyExit);
    
      /* open text file or report error */
       sp = fopen("readlist.txt", "r");
       fp = fopen("writelist.txt", "w");
    
      
        if(!sp)
        {
        perror("Error: file readlist.txt was not found or opened");
        return 0;
        }
    
    	if((fp = fopen("writelist.txt", "w"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
    	}
      /* input sentence */
     
      printf("Input a short sentence: ");
      gets(b);
     
      pch = strtok (b, " ,.-");
    
      /* numchar counts the numbers of characters in the pch string */
     numchar = countchar (pch);
      while(pch != NULL)
        {
    		/* text file comparison begin */
    		    while(fgets(filedata, 300, sp))
          {
          if(_memicmp(pch, filedata, strlen(pch)) == 0
                && (filedata[strlen(pch)] == ' '
                || filedata[strlen(pch)] == '\n'))
          {
    		  /* adding numchar to filedata erases the word and leaves the grammar intact */
    		  printf("%s\n%s\n", pch, filedata+numchar+1);
    		  /* write the text file comparison result to file */
    		  fprintf(fp, "%s\n%s\n", pch, filedata+numchar+1);
    		  break;
          }
          }
    			/* text file comparison end */
    
        /* identify the first and last letter in the word begin */
             
            red = find_letter(str2, pch);
    		if(strpbrk(str2, letters))
    		{
    		(*A)++;
    		}
    
    		if(strpbrk(str2, vowels))
    		{
    		(*B)++;
    		}
    
            reverse = rev(pch);
    
            red = find_letter(str2, pch);
    		if(strpbrk(str2, letters))
    		{
    		(*C)++;
    		}
    
    		if(strpbrk(str2, vowels))
    		{
    		(*D)++;
    		}
    		/* identify the first and last letter in the word end */
    		/* The math to see what kind of word it is start */
    		
    		if(*E = (*A && *C))
    		{
    		(*F)++;
    		}
    		
    		else if(*E = (*A && *D))
    		{
    		(*G)++;
    		}
    		
    		else if(*E = (*B && *C))
    		{
    		(*H)++;
    		}
    		else if(*E = (*B && *D))
    		{
    		(*I)++;
    		}
    		/* The math to see what kind of word it is start */
    
    		/* Reset the pointers */
    		*A = 0;
    		*B = 0;
    		*C = 0;
    		*D = 0;
    		/* print result of text file comparison */
    	
        rewind(sp);
        pch = strtok (NULL, " ,.-");
      }
      /* count the pointers */
    *J = (*F + *G + *H + *I);
    *K = ((*F)/ *J);
    *L = ((*G)/ *J);
    *M = ((*H)/ *J);
    *N = ((*I)/ *J);
      printf("\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n", *K, *L, *M, *N);
      /* write the pointers result to file */
      fprintf(fp, "\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n", *K, *L, *M, *N);
      fclose(fp);
      return 0;
    }
    
    /* The function to find the first letter in the word */
    char find_letter (char* a, char* b)
    {
        char string = strlen(b);
         
        strncpy (a,b,1);
        a[1]='\0';
        return 0;
    }
     
    /* The function to reverse the characters in a string */
    char* rev(char* str)
    {
      int end= strlen(str)-1;
      int start = 0;
     
      while( start<end )
      {
        str[start] ^= str[end];
        str[end] ^=   str[start];
        str[start]^= str[end];
     
        ++start;
        --end;
      }
     
      return str;
    }
    
    /* The function to count the number of characters in a string */
    
    int countchar (char list[])
    {
    int i, count = 0;
     
    for (i = 0; list[i] != '\0'; i++)
    count++;
    return (count);
    }
    Here is the contents of the readlist.txt file:

    Code:
    car n adj /*I know car is not a adjective*/
    ran v
    And here is the results:

    Code:
    Input a short sentence: car ran
    car
    n adj
    
    ran
    v
    
    letter vowel letter 1.000000
    letter vowel vowel 0.000000
    vowel vowel letter 0.000000
    vowel vowel vowel 0.000000
    Press any key to continue . . .
    Now I want to input a book from one copy and paste but that would give one result. I want a result per sentence and I have no idea on how to do this? Can you tell me how, please?

    Looking at the results:

    Code:
    Input a short sentence: car ran
    car
    n adj
    
    ran
    v
    
    letter vowel letter 1.000000
    letter vowel vowel 0.000000
    vowel vowel letter 0.000000
    vowel vowel vowel 0.000000
    Press any key to continue . . .
    You see car has a definition as a adjective and a noun, but words change grammar depending on how they are placed in the sentence.
    This code create the word and possible grammar in a sentence, next I can create code to filter out the adj from the result and just have
    'car
    noun'.
    But that's after I get this mess I'm in sorted out. If I can get it sorted out that is.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    You know, you should probably stop creating so many pointless variables, especially with such meaningless single-letter names.

    Code:
      float one, *A;
     
      A = &one;
     
      *A = 0;
    
            (*A)++;
    Why don't you just cut to the chase and just have
    Code:
    float one;
    one = 0;
    one++;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Quote Originally Posted by Salem View Post
    You know, you should probably stop creating so many pointless variables, especially with such meaningless single-letter names.

    Code:
      float one, *A;
     
      A = &one;
     
      *A = 0;
    
            (*A)++;
    Why don't you just cut to the chase and just have
    Code:
    float one;
    one = 0;
    one++;
    Thanks. I fixed that:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    /* function prototype to count the number of characters in a string */
    int countchar (char []);
    
    /* function prototype to find the first and last letter in the word */
     
    char find_letter (char* a, char* b);
    char red;
     
    /* function prototype to reverse the letters in the word */
     
    char* rev(char* str);
    char* reverse;
     
    /* function so I don't have to type getch all over the place */
     
    void MyExit(void) { system("pause"); }
     
    /* the main program */
     
    int main ()
    {
        /* declaring and initiaizing variables */
    	FILE *sp;
    	FILE *fp;
    	char *filedata = malloc(300);
      char str2[7];
      char * pch = malloc(300);
      char vowels[] = "aeiouy";
      char letters[] = "bcdfghjklmnpqrstvwxz";
      char *b = malloc(sizeof(char)*1000);
    
      float one;
      float two;
      float three;
      float four;
     float total;
    
     float five, *F;
      float six, *G;
      float seven, *H;
      float eight, *I;
     float total_2, *J;
     float percentage_1, *K;
     float percentage_2, *L;
     float percentage_3, *M;
     float percentage_4, *N;
     int numchar;
    
      one = 0;
      two = 0;
      three = 0;
      four = 0;
      total = 0;
    
      F = &five;
      G = &six;
      H = &seven;
      I = &eight;
      J = &total_2;
      K = &percentage_1;
      L = &percentage_2;
      M = &percentage_3;
      N = &percentage_4;
      *F = 0;
      *G = 0;
      *H = 0;
      *I = 0;
      *J = 0;
      *K = 0;
      *L = 0;
      *M = 0;
      *N = 0;
      
    
    
      atexit(MyExit);
    
      /* open text file or report error */
       sp = fopen("readlist.txt", "r");
       fp = fopen("writelist.txt", "w");
    
      
        if(!sp)
        {
        perror("Error: file readlist.txt was not found or opened");
        return 0;
        }
    
    	if((fp = fopen("writelist.txt", "w"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
    	}
      /* input sentence */
     
      printf("Input a short sentence: ");
      gets(b);
     
      pch = strtok (b, " ,.-");
    
      /* numchar counts the numbers of characters in the pch string */
     numchar = countchar (pch);
      while(pch != NULL)
        {
    		/* text file comparison begin */
    		    while(fgets(filedata, 300, sp))
          {
          if(_memicmp(pch, filedata, strlen(pch)) == 0
                && (filedata[strlen(pch)] == ' '
                || filedata[strlen(pch)] == '\n'))
          {
    		  /* adding numchar to filedata erases the word and leaves the grammar intact */
    		  printf("%s\n%s\n", pch, filedata+numchar+1);
    		  /* write the text file comparison result to file */
    		  fprintf(fp, "%s\n%s\n", pch, filedata+numchar+1);
    		  break;
          }
          }
    			/* text file comparison end */
    
        /* identify the first and last letter in the word begin */
             
            red = find_letter(str2, pch);
    		if(strpbrk(str2, letters))
    		{
    		one++;
    		}
    
    		if(strpbrk(str2, vowels))
    		{
    		two++;
    		}
    
            reverse = rev(pch);
    
            red = find_letter(str2, pch);
    		if(strpbrk(str2, letters))
    		{
    		three++;
    		}
    
    		if(strpbrk(str2, vowels))
    		{
    		four++;
    		}
    		/* identify the first and last letter in the word end */
    		/* The math to see what kind of word it is start */
    		
    		if(total = (one && three))
    		{
    		(*F)++;
    		}
    		
    		else if(total = (one && four))
    		{
    		(*G)++;
    		}
    		
    		else if(total = (two && three))
    		{
    		(*H)++;
    		}
    		else if(total = (two && four))
    		{
    		(*I)++;
    		}
    		/* The math to see what kind of word it is end */
    
    		/* Reset the pointers */
    		one = 0;
    		two = 0;
    		three = 0;
    		four = 0;
    		total = 0;
    		rewind(sp);
    
        pch = strtok (NULL, " ,.-");
      }
      /* count the pointers */
    *J = (*F + *G + *H + *I);
    *K = ((*F)/ *J);
    *L = ((*G)/ *J);
    *M = ((*H)/ *J);
    *N = ((*I)/ *J);
      printf("\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n", *K, *L, *M, *N);
      /* write the pointers result to file */
      fprintf(fp, "\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n", *K, *L, *M, *N);
      fclose(fp);
      return 0;
    }
    
    /* The function to find the first letter in the word */
    char find_letter (char* a, char* b)
    {
        char string = strlen(b);
         
        strncpy (a,b,1);
        a[1]='\0';
        return 0;
    }
     
    /* The function to reverse the characters in a string */
    char* rev(char* str)
    {
      int end= strlen(str)-1;
      int start = 0;
     
      while( start<end )
      {
        str[start] ^= str[end];
        str[end] ^=   str[start];
        str[start]^= str[end];
     
        ++start;
        --end;
      }
     
      return str;
    }
    
    /* The function to count the number of characters in a string */
    
    int countchar (char list[])
    {
    int i, count = 0;
     
    for (i = 0; list[i] != '\0'; i++)
    count++;
    return (count);
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,500
    F = &five;
    G = &six;
    H = &seven;
    I = &eight;
    J = &total_2;
    K = &percentage_1;
    L = &percentage_2;
    M = &percentage_3;
    N = &percentage_4;
    *F = 0;
    *G = 0;
    *H = 0;
    *I = 0;
    *J = 0;
    *K = 0;
    *L = 0;
    *M = 0;
    *N = 0;

    KEEP GOING!!!!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Quote Originally Posted by Salem View Post
    KEEP GOING!!!!
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    /* function prototype to count the number of characters in a string */
    int countchar (char []);
    /* function prototype to find the first and last letter in the word */
    char find_letter (char* a, char* b);
    char red;
    /* function prototype to reverse the letters in the word */
    char* rev(char* str);
    char* reverse;
    /* function so I don't have to type getch all over the place */
    void MyExit(void) { system("pause"); }
     
    /* the main program */
     
    int main ()
    {
        /* declaring and initiaizing variables */
    	FILE *sp;
    	FILE *fp;
    	char *filedata = malloc(300);
    	char str2[7];
    	char * pch = malloc(300);
    	char vowels[] = "aeiouy";
    	char letters[] = "bcdfghjklmnpqrstvwxz";
    	char *b = malloc(sizeof(char)*1000);
    	float one;
    	float two;
    	float three;
    	float four;
    	float total;
    	float five;
    	float six;
    	float seven;
    	float eight;
    	float total_2;
    	float percentage_1;
    	float percentage_2;
    	float percentage_3;
    	float percentage_4;
    	int numchar;
    	one = 0;
    	two = 0;
    	three = 0;
    	four = 0;
    	total = 0;
    	five = 0;
    	six = 0;
    	seven = 0;
    	eight = 0;
    	total_2 = 0;
    	percentage_1 = 0;
    	percentage_2 = 0;
    	percentage_3 = 0;
    	percentage_4 = 0;
    	atexit(MyExit);
    	/* open text file or report error */
       sp = fopen("readlist.txt", "r");
       fp = fopen("writelist.txt", "w");
    
        if(!sp)
        {
        perror("Error: file readlist.txt was not found or opened");
        return 0;
        }
    	if((fp = fopen("writelist.txt", "w"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
    	}
    
      /* input sentence */
     
      printf("Input a short sentence: ");
      gets(b);
      pch = strtok (b, " ,.-");
    
      /* numchar counts the numbers of characters in the pch string */
     numchar = countchar (pch);
      while(pch != NULL)
        {
    		/* text file comparison begin */
    		while(fgets(filedata, 300, sp))
    		{
    			if(_memicmp(pch, filedata, strlen(pch)) == 0
    				&& (filedata[strlen(pch)] == ' '
    				|| filedata[strlen(pch)] == '\n'))
    			{
    				/* adding numchar to filedata erases the word and leaves the grammar intact */
    				printf("%s\n%s\n", pch, filedata+numchar+1);
    				/* write the text file comparison result to file */
    				fprintf(fp, "%s\n%s\n", pch, filedata+numchar+1);
    			}
    		}
    		/* text file comparison end */
    		
    		/* identify the first and last letter in the word begin */
    		
    		red = find_letter(str2, pch);
    		if(strpbrk(str2, letters))
    		{
    		one++;
    		}
    		if(strpbrk(str2, vowels))
    		{
    		two++;
    		}
    
            reverse = rev(pch);
    
            red = find_letter(str2, pch);
    		if(strpbrk(str2, letters))
    		{
    		three++;
    		}
    		if(strpbrk(str2, vowels))
    		{
    		four++;
    		}
    		/* identify the first and last letter in the word end */
    		/* The math to see what kind of word it is start */
    		
    		if(total = (one && three))
    		{
    		five++;
    		}
    		else if(total = (one && four))
    		{
    		six++;
    		}
    		else if(total = (two && three))
    		{
    		seven++;
    		}
    		else if(total = (two && four))
    		{
    		eight++;
    		}
    		/* The math to see what kind of word it is end */
    
    		/* Reset the pointers */
    		one = 0;
    		two = 0;
    		three = 0;
    		four = 0;
    		total = 0;
    		rewind(sp);
    
        pch = strtok (NULL, " ,.-");
      }
      
      /* count the pointers */
      total_2 = (five + six + seven + eight);
      percentage_1 = ((five)/ total_2);
      percentage_2 = ((six)/ total_2);
      percentage_3 = ((seven)/ total_2);
      percentage_4 = ((eight)/ total_2);
      printf("\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n", percentage_1, percentage_2, percentage_3, percentage_4);
      /* write the pointers result to file */
      fprintf(fp, "\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n", percentage_1, percentage_2, percentage_3, percentage_4);
      fclose(fp);
      return 0;
    }
    
    /* The function to find the first letter in the word */
    char find_letter (char* a, char* b)
    {
        char string = strlen(b);
        strncpy (a,b,1);
        a[1]='\0';
        return 0;
    }
     
    /* The function to reverse the characters in a string */
    char* rev(char* str)
    {
      int end= strlen(str)-1;
      int start = 0;
     
      while( start<end )
      {
        str[start] ^= str[end];
        str[end] ^=   str[start];
        str[start]^= str[end];
     
        ++start;
        --end;
      }
     
      return str;
    }
    
    /* The function to count the number of characters in a string */
    
    int countchar (char list[])
    {
    	int i, count = 0;
    	for (i = 0; list[i] != '\0'; i++)
    		count++;
    	return (count);
    }
    Thanks for the input, now the code is easier to read!
    Edit,
    I think the answer to how to run the code on a sentence at a time, not the whole input at a time has something to do with a new while loop, but I'm not sure on how to make it.
    Last edited by jeremy duncan; 11-02-2011 at 01:49 PM.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You'll probably ignore this (again), but hope springs eternal.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    int main (void) {
       int i, j, len;
       char str[]= {"There is unrest in the forest, there is trouble with the trees\nfor the maples want more sunlight and the oaks ignore their pleas.."};
    /*
          The Trees 
    
    There is unrest in the forest, there is trouble with the trees 
    for the maples want more sunlight and the oaks ignore their pleas..
    
    The trouble with the maples, and they're quite convinced they're right,
    they say the oaks are just too lofty and grab up all the light. 
    
    The oaks can't help their feelings if they like the way they're made, 
    and they wonder why the maples can't be happy in the shade?
    
    There is trouble in the forest and the creatures all have fled, 
    as the maples scream "OPPRESSION" and the oaks just shake their heads ..
    
    So the maples formed a union and demanded equal rights, 
    the oaks are just too greedy. We will make them give us light.
    
    Now there is no more oak oppression for they passed a noble law, 
    now the trees are all kept equal by hatchet, axe and saw... "};
    */
       printf("%s\n\n",str);
          
       j=0;
       len = strlen(str)+1;
       for(i=0;i<len;i++) {
          if(str[i]=='\n')
             putchar('\n');
    
          if(isalpha(str[i])) {
             if(str[i+1]==' ' || ispunct(str[i+1]) || str[i+1]=='\n') {
                putchar(str[i]);
                if(ispunct(str[i+1]))
                   putchar(' ');
             }
             if(j==0)
                putchar(str[i]);
             else
                putchar(' ');          
             ++j;
          }
          else {
             j=0;
          }
    
       }
       printf("\n\n");
       return 0;
    }
    jeremy duncan likes this.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I have some functions I googled that will allow me to feed in a line at a time into the program, but I still don't know how to call it in the main program.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    /* the googled code start */
    #define BUFFMT "%255"
    #define LINE_LEN 256
    #define START_COUNT 1
    
    FILE *OpenFile(const char *fileName)
    {
        FILE *fptr;
        if ((fptr = fopen(fileName, "r")) == NULL) {
            fprintf(stderr, "Error opening file %s, exiting...",  fileName);
            exit(EXIT_FAILURE);
        }
        return fptr;
    }
    
    LIST *CreateList(FILE *fp) 
    {
        char buf[LINE_LEN];
    
        while (fscanf(fp, BUFFMT"s", buf) != EOF) {
            printf("%s: \n", buf);
        }
    }
    /* the googled code end */
    /* function prototype to count the number of characters in a string */
    int countchar (char []);
    /* function prototype to find the first and last letter in the word */
    char find_letter (char* a, char* b);
    char red;
    /* function prototype to reverse the letters in the word */
    char* rev(char* str);
    char* reverse;
    /* function so I don't have to type getch all over the place */
    void MyExit(void) { system("pause"); }
     
    /* the main program */
     
    int main ()
    Adak, I ran your program and I got this output:

    Code:
    There is unrest in the forest, there is trouble with the trees
    for the maples want more sunlight and the oaks ignore their pleas..
    
    T   e is u    t in t e f    t  t   e is t     e w  h t e t   s
    f r t e m    s w  t m  e s      t a d t e o  s i    e t   r p   s
    I'm not sure what you mean by showing me this?

    Could you show me how to call those functions in my code?
    What I will have to do is save the book to a text file.
    Then use the functions I googled, calling them in the main program.
    What should happen is I feed in the text file with the book into the program, one line at a time.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I wrote my own code to send a while loop a sentence one at a time from a text file.

    Here it is:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main ()
    {
    	/* declare and initializing variables */
    	FILE *fp;
    	char *filedata = malloc(300);
    	char *buf;
    	int str2;
    	str2=0;
    
    	/* open file or report error */
    	fp = fopen("readlist.txt", "r");
    	 if(!fp)
        {
        perror("Error: file readlist.txt was not found or opened");
        return 0;
        }
    	/* send the text from the textfile into the filedata variable */
    	 fgets(filedata, 300, fp);
    	 /* tokenize sentence */
    	buf = strtok(filedata, ".");
    	/* loop through each sentence. Each loop adds 1 to the str2 variable, this is to show the loop only ran one of the sentences. */
    	while(buf!=NULL)
    	{
    		str2++;
            printf("%s %d\n", buf, str2);
    		buf = strtok(NULL, ".");
    	}
    	return 0;
    }
    The contents of the text file:

    Code:
    car n adj. ran v. car n adj. ran v. car n adj. ran v. car n adj. ran v.
    And finally the results:

    Code:
    car n adj 1
     ran v 2
     car n adj 3
     ran v 4
     car n adj 5
     ran v 6
     car n adj 7
     ran v 8
    Now I have to figure out how to add this code to the code I'm trying to fix. So far so good.
    Last edited by jeremy duncan; 11-02-2011 at 11:46 PM. Reason: reworded the first sentence.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I spent a few hours trying to add my solution from my previous post the the code I'm trying to update.
    Either I'm too tired to do it or I'm too much of a newbie. Can you please try and answer the question I made this thread to answer?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The program I posted is a fairly good way to pick out the first and last letters of each word. With the horizontal display, it's pretty easy to check if it's accurate or not. An even better display would put the first and last letters into an array of char's, and print them out right under the full line of text they were taken from.

    If you use strtok(), be warned - it changes the string it's working from! You won't like that.

    Using fgets() is the way to go!

    I do wish you'd STOP coding, and just work on what you want the flow of the program, to be. Step1, step2, step3, etc. Pushing on the cart is OK in a sense -- it will get you going in the right direction. However, it's a hell of a lot easier, if you work out the harness and reins, and hitch the horse to the cart, and then ride, instead of pushing.

    By that, I mean you're going at this, backwards, and it will only continue to make your project a lot more work than would otherwise be needed.

    I know you're new, but you know already that you have to plan things before you start building them. That's what you need here -- "blueprints" for your program!

    If you go to the Guttenberg project, you can download lots of books, for free.

    "results for every string", is way to vague to get good specific help. Put down in a reply, a specific input sentence that is failing now, and what part of your code should handle it, and is not able to do so.

    Input that fails, and output you want for that input.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Hi Adak,

    I tweaked you code a bit so it's easier to read on my PS3 browser.
    And I added a line so the output is a words beginning and end per line.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
     
    int main (void) {
       int i, j, len;
       char str[]= {"There is unrest in the forest, there is trouble with the trees\nfor the maples want more sunlight and the oaks ignore their pleas.."};
    
       printf("%s\n\n",str);
           
       j=0;
       len = strlen(str)+1;
       for(i=0;i<len;i++) 
       {
          if(str[i]=='\n')
             putchar('\n');
     
          if(isalpha(str[i])) 
          {
             if(str[i+1]==' ' || ispunct(str[i+1]) || str[i+1]=='\n') 
             {
                putchar(str[i]);
                if(ispunct(str[i+1]))
                   putchar(' ');
                   putchar('\n');
             }
             if(j==0)
                putchar(str[i]);
             else
                putchar(' ');         
             ++j;
          }
          else 
          {
             j=0;
          }
     
       }
       printf("\n\n");
       getch();
       return 0;
    }
    And the results:

    Code:
    There is unrest in the forest, there is trouble with the trees
    for the maples want more sunlight and the oaks ignore their pleas..
    
    T   e
     is
     u    t
     in
     t e
     f    t
     t   e
     is
     t     e
     w  h
     t e
     t   s
    
    f r
     t e
     m    s
     w  t
     m  e
     s      t
     a d
     t e
     o  s
     i    e
     t   r
     p   s
    I will try and use this in my code instead of strtok. But first I need to learn how your code works and then I may begin replacing the strtok in my code with the knowledge this code provided.

    I will stop coding until I can understand the code I want to implement.
    Thank you very much for your input.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
     
    int main (void) {
       int character, J, len;
       char string[]= {"There is forest."};
    
       printf("%s\n\n",string);
           
       J=0;
       len = strlen(string)+1;
       for(character=0;character<len;character++) 
       {
            if(string[character]=='\n')
            putchar('\n');
     
            if(isalpha(string[character])) 
            {
                 if(string[character+1]==' ' || ispunct(string[character+1]) || string[character+1]=='\n') 
                 {
                                           putchar(string[character]);
                                           if(ispunct(string[character+1]))
                                           putchar(' ');
                                           putchar('\n');
                 }
                 if(J==0)
                 putchar(string[character]);
                 
                 else
                 putchar(' ');
                 ++J;
            }
            else 
            {
                 J=0;
            }
     
       }
       printf("\n\n");
       getch();
       return 0;
    }
    This is the previous code, but it should be easier to read on my ps3 browser.

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I made a YouTube video that shows my program being debugged. Watch it so you can see what happens step by step.
    Debugging C program

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I watched the video, and commented on it in the thread you started, about it.

    I can't figure out why you want to program on a PS3 browser which has (apparently), a very small screen size.

    The original display was good because it lined up the first and last letters, right beneath the original word. Now, you have it all stacked up over on the far left hand side, and it is quite odd. (but I'm not viewing it on a PS3 console screen, either).

    The best way to understand the logic in the program, is to make a flowchart diagram of it. It may not help you code directly, but it will show you the logic used, and that should in turn, help your coding efforts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sentence in a string.
    By bnkslo in forum C Programming
    Replies: 5
    Last Post: 04-17-2008, 11:48 AM
  2. Help with Sentence fix string program
    By daywalker- in forum C++ Programming
    Replies: 9
    Last Post: 11-01-2007, 06:44 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. unexpected result, no display of input string
    By xephyr in forum C Programming
    Replies: 11
    Last Post: 08-04-2004, 07:22 PM
  5. Replies: 10
    Last Post: 03-19-2003, 01:15 PM

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