I made a program but it doesn't work correctly

This is a discussion on I made a program but it doesn't work correctly within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <stdlib.h> void MyExit(void) { system("pause"); } int main () { char *b = malloc(sizeof(char)*1000); ...

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

    I made a program but it doesn't work correctly

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void MyExit(void) { system("pause"); }
    
    int main ()
    {
      char *b = malloc(sizeof(char)*1000);
      char *filedata_2 = malloc(300);
      FILE *sp;
      char * pch = malloc(300);
      char alpha[] = "101";
      char beta[] = "100";
      char charlie[] = "001";
      int echo;
      int delta;
      int llamba;
      int one, *A;
      int two, *B;
      int three, *C;
      int total = 0;
      int tab = 0;
    
      atexit(MyExit);
    
      A = &one;
      B = &two;
      C = &three;
      *A = 0;
      *B = 0;
      *C = 0;
      sp = fopen("worklist_2.txt", "r");
      echo = atoi (alpha);
      delta = atoi (beta);
      llamba = atoi (charlie);
      if(!sp)
        {
        perror("Error: file worklist_2.txt was not found or opened");
        return 0;
        }
    
      printf("Input a short sentence: ");
      gets(b);
    
      pch = strtok (b," ,.-");
      while(pch != NULL)
        {
        while(fgets(filedata_2, 300, sp))
          {
          if(_memicmp(pch, filedata_2, strlen(pch)) == 0
                && (filedata_2[strlen(pch)] == ' '
                || filedata_2[strlen(pch)] == '\n'))
    	  {
    		  tab = 1;
    		  break;	  		  
    	  }
          else
            {
            strcpy(filedata_2, pch);
            strcat(filedata_2, " No match!\n");
            }
          }
    	if(memchr (filedata_2, echo, strlen(filedata_2)))
    		{
    			  *A+= 1;
    		}
    	if(memchr (filedata_2, delta, strlen(filedata_2)))
    		{
    			 *B+= 1;
    		}
    	if(memchr (filedata_2, llamba, strlen(filedata_2)))
    		{
    			 *C+= 1;
    		}
    
    
        printf("%s\n",filedata_2);
        rewind(sp);
        pch = strtok (NULL, " ,.-");
      }
      total = (one+two+three);
      printf("%d\n",total);
      return 0;
    }
    The text file
    Code:
    we 100
    
    met 101
    
    a 101
    
    young 101
    
    street 101
    
    child 101
    
    he 100
    
    was 101
    
    playing 101
    
    what 101
    
    looked 101
    
    like 100
    
    a 101
    
    wooden 101
    
    flute 100
    The input string
    Code:
    We met a young street child, he was playing what looked like a wooden flute.
    And the results:
    Code:
    Input a short sentence: We met a young street child, he was playing what looked
    like a wooden flute.
    we 100
    
    met 101
    
    a 101
    
    young 101
    
    street 101
    
    child 101
    
    he 100
    
    was 101
    
    playing 101
    
    what 101
    
    looked 101
    
    like 100
    
    a 101
    
    wooden 101
    
    flute 100
    11
    Press any key to continue . . .
    There's 15 words and the program is only reporting 11 of them. I don't know why and I have been sitting here since this morning about 15 hours ago working on this. Can you help me fix this program?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Based on your input and output:

    We != we
    he is after a ", "
    like appears to have a \r before it.
    flute has a . on the end.


    Look at your strtok process and see if it is really doing what you want.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I think I found the problem, I'm not resetting the A, B,C pointers in the while loop.
    Any help on this problem would be nice.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    quzah, that's not the problem, i tested it but thanks.

    How I define resetting the pointer is when a match is found for *A and one is incremented one, then the word next word in the pch while loop starts at the top of the text file, not where the previous word was found.



    An example; if the input word 'a' is used in the pch loop then the word 'a 101' is found in the text file by starting at the top of the text file and then reading each line until 'a 101' is found.

    Then the next input word 'young' is in the pch while loop, the text file can either start searching from the top of the list, or at the word 'a 101'. To search at the top of the list I have to reset the pointer, I think.

    edit,

    The code in the first post reports 15 words if I use 15 of the same words:

    met met met met met met met met met met met met met met met

    I wonder what is wrong with the code? I guess I will have to leave it and learn more C and in a year or two I should have my answer?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hey Jeremy, what are you trying to do here? Count words in the text and something else?

    strtok() makes changes to the text it is tokenizing, which doesn't lead to repeated searching of the string, being what you might expect.

    Let's back away from the how, and go back to the what you want to do, and we'll find the how that's suitable.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I have been busy, I gave up on how I was incrementing *A, *B, *C.
    Now the code I made is the building blocks I will play with tomorrow to get *A, *B, *C to increment.

    There's two codes, each one gives me a result, I will try and join the codes tomorrow so one code gives me the two results i will show you now:

    result 1:

    Code:
    Input a short sentence: andy
    andy
    a
    Press any key to continue . . .
    result 2:

    Code:
    Input a short sentence: andy
    ydna
    y
    Press any key to continue . . .
    These two results show me if there is a vowel on the tip of the word.
    The idea is this:
    If there is a vowel on the left of the word but not on the right of the words the number value is 001.
    If there is a vowel on the right of the word but not on the left of the words the number value is 100.
    If there is no vowel on either far side of the word the number value is 101.

    When there is a number: 101, 100, 001 then one of the pointers increments by 1. This way each input word has a value. The total number of words then can be used to calculate the percentage a number was used in a sentence.
    The theory is stories begin with the larger percentage being 100 words, then as the story ends and it's a fairly large story the percentage of words per sentence is largely 001.

    This is to help me with my AI project I'm piecing together bit by bit.

    Here are the two codes, they're pretty big though:

    code 1:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    void MyExit(void) { system("pause"); }
    
    int main ()
    {
      char *b = malloc(sizeof(char)*1000);
      char *filedata_2 = malloc(300);
      FILE *sp;
      char * pch = malloc(300);
      char alpha[] = "101";
      char beta[] = "100";
      char charlie[] = "001";
      char vowels[] = "aeiouy";
      char str2[7];
      char *v  = malloc(300);
      int echo;
      int delta;
      int llamba;
      int one, *A;
      int two, *B;
      int three, *C;
      int total = 0;
      int tab = 0;
    
      atexit(MyExit);
    
      A = &one;
      B = &two;
      C = &three;
      *A = 0;
      *B = 0;
      *C = 0;
      sp = fopen("worklist_2.txt", "r");
      echo = atoi (alpha);
      delta = atoi (beta);
      llamba = atoi (charlie);
      if(!sp)
        {
        perror("Error: file worklist_2.txt was not found or opened");
        return 0;
        }
    
      printf("Input a short sentence: ");
      gets(b);
    
      pch = strtok (b, " ,.-");
      while(pch != NULL)
        {
        while(fgets(filedata_2, 300, sp))
          {
          if(_memicmp(pch, filedata_2, strlen(pch)) == 0
                && (filedata_2[strlen(pch)] == ' '
                || filedata_2[strlen(pch)] == '\n'))
    	  {
    		  tab = 1;
    		  break;	  		  
    	  }
          else
            {
            strcpy(filedata_2, pch);
            strcat(filedata_2, " No match!\n");
            }
          }
    	strncpy (str2,pch,1);
    	str2[1]='\0';
    	v = strpbrk (str2, vowels);
        v = strpbrk (v,vowels);
        printf("%s\n%s\n",pch, v);
        rewind(sp);
        pch = strtok (NULL, " ,.-");
      }
    
      return 0;
    }
    code 2:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    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;
    }
    
    void MyExit(void) { system("pause"); }
    
    int main ()
    {
      FILE *sp;
      char alpha[] = "101";
      char beta[] = "100";
      char charlie[] = "001";
      char vowels[] = "aeiouy";
      char str2[7];
      int echo;
      int delta;
      int llamba;
      int one, *A;
      int two, *B;
      int three, *C;
      int total = 0;
      int tab = 0;
      
      char * pch = malloc(300);
      char *v  = malloc(300);
      char *k  = malloc(300);
      char *b = malloc(sizeof(char)*1000);
      char *filedata_2 = malloc(300);
    
      atexit(MyExit);
    
      A = &one;
      B = &two;
      C = &three;
      *A = 0;
      *B = 0;
      *C = 0;
      sp = fopen("worklist_2.txt", "r");
      echo = atoi (alpha);
      delta = atoi (beta);
      llamba = atoi (charlie);
      if(!sp)
        {
        perror("Error: file worklist_2.txt was not found or opened");
        return 0;
        }
    
      printf("Input a short sentence: ");
      gets(b);
    
      pch = strtok (b, " ,.-");
      while(pch != NULL)
        {
        while(fgets(filedata_2, 300, sp))
          {
          if(_memicmp(pch, filedata_2, strlen(pch)) == 0
                && (filedata_2[strlen(pch)] == ' '
                || filedata_2[strlen(pch)] == '\n'))
    	  {
    		  tab = 1;
    		  break;	  		  
    	  }
          else
            {
            strcpy(filedata_2, pch);
            strcat(filedata_2, " No match!\n");
            }
          }
    	k=(rev(pch));
    	strncpy (str2,k,1);
    	str2[1]='\0';
    	v = strpbrk (str2, vowels);
        v = strpbrk (v,vowels);
        printf("%s\n%s\n",k, v);
        rewind(sp);
        pch = strtok (NULL, " ,.-");
      }
    
      return 0;
    }
    I'm posting these codes even though they are large because if your trying to solve this problem I thought I should help you by giving you all the info I have. So far it's looking good though!

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just a quick note at first, you describe 001 as a number, but your program is treating all the type numbers(100, 101, 001) as strings of char's, not numbers. Also, 001 is not a number, and if you ask the computer what it is, it will tell you it's just 1.

    Is that OK with your program plans?

    For this vowel problem, it's simple, but let's look at it outside the complexities of your program.

    You have a short sentence:
    Andy has a red bicycle, but Joey doesn't.

    You want to separate each word in the sentence, and then see which type of word it is, based on the type of letter in the first and last letter of each word, right?

    How do you want to handle one letter words like " a " or " I "?

    It would help if this was in a separate function, so as not to further complicate your main function.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Here is the code that outputs the two results from the same code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    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;
    }
    
    void MyExit(void) { system("pause"); }
    
    int main ()
    {
      char alpha[] = "101";
      char beta[] = "100";
      char charlie[] = "001";
      char vowels[] = "aeiouy";
      char str2[7];
      int echo;
      int delta;
      int llamba;
      int total = 0;
      int tab = 0;
    
      char *b = malloc(sizeof(char)*1000);
      char *filedata_2 = malloc(300);
      char * pch = malloc(300);
      char *v  = malloc(300);
      char *j  = malloc(300);
      char *k  = malloc(300);
      int one, *A;
      int two, *B;
      int three, *C;
      FILE *sp;
    
      atexit(MyExit);
    
      A = &one;
      B = &two;
      C = &three;
      *A = 0;
      *B = 0;
      *C = 0;
      sp = fopen("worklist_2.txt", "r");
      echo = atoi (alpha);
      delta = atoi (beta);
      llamba = atoi (charlie);
      if(!sp)
        {
        perror("Error: file worklist_2.txt was not found or opened");
        return 0;
        }
    
      printf("Input a short sentence: ");
      gets(b);
    
      pch = strtok (b, " ,.-");
      while(pch != NULL)
        {
        while(fgets(filedata_2, 300, sp))
          {
          if(_memicmp(pch, filedata_2, strlen(pch)) == 0
                && (filedata_2[strlen(pch)] == ' '
                || filedata_2[strlen(pch)] == '\n'))
    	  {
    		  tab = 1;
    		  break;	  		  
    	  }
          else
            {
            strcpy(filedata_2, pch);
            strcat(filedata_2, " No match!\n");
            }
          }
    
    	if(memcmp(v, j, strlen(v))==0)
    	{
    		strncpy (str2,pch,1);
    		str2[1]='\0';
    		v = strpbrk (str2, vowels);
    		j = strpbrk (v,vowels);
    		printf("%s\n%s\n",pch, j);
    	}
     	if(memcmp(v, j, strlen(v))==0)
    	{
    		k=(rev(pch));
    		strncpy (str2,k,1);
    		str2[1]='\0';
    		v = strpbrk (str2, vowels);
    		j = strpbrk (v,vowels);
    		printf("%s\n%s\n",pch, j);
    	}
        rewind(sp);
        pch = strtok (NULL, " ,.-");
      }
    
      return 0;
    }
    The results:
    Code:
    Input a short sentence: andy
    andy
    a
    ydna
    y
    Press any key to continue . . .
    But it crashes if the input word doesn't have a vowel on either side of the word? Can somebody help me fix this?

    The 101, 001, 100 are graphical representations of the way the word uses the vowel. It is not what is counted when calculating the percentage when I make that part of the code.

    Single letter words are treated like they are 101. (A word made 100% by a vowel) = 101.

    Once I get this crashing problem fixed I can start incrementing pointer then counting the input strings. Any help to fix this crashing if a word doesn't have a vowel on both sides of the word would be appreciated.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    This code works and I think I can fix the if statements to fix the crashing bug:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    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;
    }
    
    void MyExit(void) { system("pause"); }
    
    int main ()
    {
      char alpha[] = "101";
      char beta[] = "100";
      char charlie[] = "001";
      char vowels[] = "aeiouy";
      char str2[7];
      int echo;
      int delta;
      int llamba;
      int total = 0;
      int tab = 0;
    
      char *b = malloc(sizeof(char)*1000);
      char *filedata_2 = malloc(300);
      char * pch = malloc(300);
      char *v  = malloc(300);
      char *j  = malloc(300);
      char *k  = malloc(300);
      int one, *A;
      int two, *B;
      int three, *C;
      FILE *sp;
    
      atexit(MyExit);
    
      A = &one;
      B = &two;
      C = &three;
      *A = 0;
      *B = 0;
      *C = 0;
      sp = fopen("worklist_2.txt", "r");
      echo = atoi (alpha);
      delta = atoi (beta);
      llamba = atoi (charlie);
      if(!sp)
        {
        perror("Error: file worklist_2.txt was not found or opened");
        return 0;
        }
    
      printf("Input a short sentence: ");
      gets(b);
    
      pch = strtok (b, " ,.-");
      while(pch != NULL)
        {
        while(fgets(filedata_2, 300, sp))
          {
          if(_memicmp(pch, filedata_2, strlen(pch)) == 0
                && (filedata_2[strlen(pch)] == ' '
                || filedata_2[strlen(pch)] == '\n'))
    	  {
    		  tab = 1;
    		  break;	  		  
    	  }
          else
            {
            strcpy(filedata_2, pch);
            strcat(filedata_2, " No match!\n");
            }
          }
    	/* part of code to update: begin */
    
    	strncpy (str2,pch,1);
    	str2[1]='\0';
    	v = strpbrk (str2, vowels);
    	j = strpbrk (v,vowels);
    	if(memcmp(v, j, strlen(v))==0)
    	{
    		printf("%s\n%s\n",pch, j);
    	}
    
    	/* separating the parts */
    
    	k=(rev(pch));
    	strncpy (str2,k,1);
    	str2[1]='\0';
    	v = strpbrk (str2, vowels);
    	j = strpbrk (v,vowels);
     	if(memcmp(v, j, strlen(v))==0)
    	{
    		printf("%s\n%s\n",pch, j);
    	}
    	/* part of code to update: end */
    
        rewind(sp);
        pch = strtok (NULL, " ,.-");
      }
    
      return 0;
    }

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Quote Originally Posted by Adak View Post
    It would help if this was in a separate function, so as not to further complicate your main function.
    I have made the function as you advised. The entire code is here:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    /* function to find the first and last letter in the word */
    
    void find_letter (char* a, char* b, char* c, char* d, char* e)
    {
    	char string = strlen(b);
    	
    	strncpy (a,b,1);
    	a[1]='\0';
    	c = strpbrk (a, d);
    	e= strpbrk (c, d);
    	printf("%s\n", e);
    	
    }
    
    //The pattern
    //strncpy (str2,pch,1);
    //	str2[1]='\0';
    //	v = strpbrk (str2, vowels);
    //	j = strpbrk (v,vowels);
    //    printf("%s\n%s\n",pch, j);
    
    //find_letter(str2, pch, l, letters, j);
    
    /* function to reverse the letters in the word */
    
    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;
    }
    
    /* 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 */
    
      char alpha[] = "101";
      char beta[] = "100";
      char charlie[] = "001";
      char vowels[] = "aeiouy";
      char letters[] = "bcdfghjklmnpqrstvwxz";
      char str2[7];
      int echo;
      int delta;
      int llamba;
      int total = 0;
      int tab = 0;
    
      char *b = malloc(sizeof(char)*1000);
      char *filedata_2 = malloc(sizeof(char)*1000);
      char * pch = malloc(sizeof(char)*1000);
      char *v = malloc(sizeof(char)*1000);
      char *j = malloc(sizeof(char)*1000);
      char *k = malloc(sizeof(char)*1000);
      char *l = malloc(sizeof(char)*1000);
      char *r = malloc(sizeof(char)*1000);
      int one, *A;
      int two, *B;
      int three, *C;
      FILE *sp;
    
      atexit(MyExit);
      A = &one;
      B = &two;
      C = &three;
      *A = 0;
      *B = 0;
      *C = 0;
      sp = fopen("worklist_2.txt", "r");
      echo = atoi (alpha);
      delta = atoi (beta);
      llamba = atoi (charlie);
      if(!sp)
        {
        perror("Error: file worklist_2.txt was not found or opened");
        return 0;
        }
    
      /* input sentence */
    
      printf("Input a short sentence: ");
      gets(b);
    
      pch = strtok (b, " ,.-");
    
      while(pch != NULL)
        {
    		/* compare input word to word in text file */
    
        while(fgets(filedata_2, 300, sp))
          {
          if(_memicmp(pch, filedata_2, strlen(pch)) == 0
                && (filedata_2[strlen(pch)] == ' '
                || filedata_2[strlen(pch)] == '\n'))
    	  {
    		  tab = 1;
    		  break;	  		  
    	  }
          else
            {
            strcpy(filedata_2, pch);
            strcat(filedata_2, " No match!\n");
            }
          }
    	/* identify the first and last letter in the word */
    
    	find_letter(str2, pch, l, letters, j);
    	rev(pch);
    	find_letter(str2, pch, l, letters, j);
    	
    	/* necessary stuff but not what the program is about */
    
        rewind(sp);
        pch = strtok (NULL, " ,.-");
      }
    
      return 0;
    }
    Here is the results:

    Code:
    Input a short sentence: good stuff stan
    g
    d
    s
    f
    s
    n
    Press any key to continue . . .
    I can change

    Code:
    find_letter(str2, pch, l, letters, j);
    rev(pch);
    find_letter(str2, pch, l, letters, j);
    to
    Code:
    find_letter(str2, pch, l, vowels, j);
    rev(pch);
    find_letter(str2, pch, l, vowels, j);
    But I can't do this:
    Code:
    find_letter(str2, pch, l, letters, j);
    find_letter(str2, pch, l, vowels, j);
    rev(pch);
    find_letter(str2, pch, l, letters, j);
    find_letter(str2, pch, l, vowels, j);
    Can you help me fix this problem, Adak?

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    If I change the function I can mix the two results in the while loop.
    Here is the new function from the code:

    Code:
    char find_letter (char* a, char* b, char* c, char* d, char* e)
    {
    	char string = strlen(b);
    	
    	strncpy (a,b,1);
    	a[1]='\0';
    	c = strpbrk (a, d);
    	e= strpbrk (c, d);
    	printf("%s\n", e);
    	return 0;
    }
    And here it is in the while loop:

    Code:
    /* identify the first and last letter in the word */
    
    	find_letter(str2, pch, l, letters, j);
    	rev(pch);
    	find_letter(str2, pch, v, vowels, j);
    
    	
    	/* necessary stuff but not what the program is about */
    The results:
    Code:
    Input a short sentence: quickly the bee gave the chase
    q
    y
    t
    e
    b
    e
    g
    e
    t
    e
    c
    e
    Press any key to continue . . .
    Before I had to use both letters or both vowels, so this is a step up from where I was.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Jeremy, you're flitting from flower to flower, and explaining problems in terms of your code.

    No good.

    To help, I need you to take ONE problem at a time, and concentrate on that problem only, and explain the problem in specific english, and hopefully show an example.

    Like this:
    Code:
    Input a short sentence: good stuff stan
    g
    d
    s
    f
    s
    n
    Press any key to continue . . .
    I thought the problem was getting a pattern number for each word, based on the placement of the vowels on the first and the last letter of the word. The above shows you're getting the first and last letter of each word, (at least if the first and last letters of each word are consonants). **

    Let me know in English what the specific problem is you want help with. I've obviously fallen behind just where you are in this program.

    ** What happens when you have some words that begin and/or end with a vowel, or a vowel on the first letter, and a consonant on the last, or repeating letters, or words with just one letter, etc?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    @Jeremy ... Make a PLAN, son... Make a PLAN...

    Nobody ever wrote decent code without some kind of pre-arranged goal and layout of what they're coding.

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I made some code changes so the results are good if the word has a vowel as the first or last letter, or a consonant as the first or last letter.

    But if the code specifies the word has a vowel as the first letter in the word and a consonant as the last letter in the word if I input a different kind of word the program crashes, and I don't know how to fix this yet.

    I cleaned up the code to, removing old code.

    What I am trying to do with this code is see if the word has a consonant or vowel as the first or last letter in the word, and based on that incrementing the pointer (*A, *B, *C).
    The pointers are classified as (101, 001, 100) in a printf maybe, but after all the coding is done and the results just need to be printed on-screen.

    Then with all the words counted in the pointers (*A, *B, *C), I make the pointers have percentage values.

    Here is the code I have so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    /* function prototype to find the first and last letter in the word */
    
    char find_letter (char* a, char* b, char* c, char* d, char* e);
    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 */
    
      char str2[7];
      char * pch = malloc(300);
      char *l  = malloc(300);
      char *v  = malloc(300);
      char vowels[] = "aeiouy";
      char letters[] = "bcdfghjklmnpqrstvwxz";
      char *j  = malloc(300);
      char *b = malloc(sizeof(char)*1000);
    
      atexit(MyExit);
    
      /* input sentence */
    
      printf("Input a short sentence: ");
      gets(b);
    
      pch = strtok (b, " ,.-");
    
      while(pch != NULL)
        {
    	/* identify the first and last letter in the word */
    
    	red = find_letter(str2, pch, l, letters, j);
    	reverse = rev(pch);
    	red = find_letter(str2, pch, v, vowels, j);
    	
    	/* necessary stuff */
    
        pch = strtok (NULL, " ,.-");
      }
    
      return 0;
    }
    
    /* The function to find the first letter in the word */
    char find_letter (char* a, char* b, char* c, char* d, char* e)
    {
    	char string = strlen(b);
    	
    	strncpy (a,b,1);
    	a[1]='\0';
    	c = strpbrk (a, d);
    	e= strpbrk (c, d);
    	printf("%s\n", e);
    	e;
    	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;
    }
    Here is the results:

    Code:
    Input a short sentence: jeremy kindly type code
    j
    y
    k
    y
    t
    e
    c
    e
    Press any key to continue . . .
    Can somebody help me? I have been doing all this all by myself, I googled the reverse code but the rest is of my design.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't understand what is going on here, but strlen() returns the length of a string (minus the end of string char), which is a size_t. A size_t is an int for our purposes.

    Why assign an int, to a char? Char's can't hold an int unless it is either between CHAR_MIN to CHAR_MAX or UCHAR_MIN to UCHAR_MAX (if it's unsigned).

    You can see what those values are on your system, by including <limits.h> and using a printf("%d", CHAR_MAX);, etc.


    Code:
    char find_letter (char* a, char* b, char* c, char* d, char* e)
    {
    	char string = strlen(b);
    	
    	strncpy (a,b,1);
    	a[1]='\0';
    	c = strpbrk (a, d);
    	e= strpbrk (c, d);
    	printf("%s\n", e);
    	e;
    	return 0;
    }
    I'll look at it some more when I get back.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why doesn't this c program work?
    By hobolux in forum C Programming
    Replies: 16
    Last Post: 01-17-2011, 02:05 PM
  2. Uploading files doesn't work correctly
    By Devils Child in forum C# Programming
    Replies: 8
    Last Post: 05-21-2009, 07:51 AM
  3. Writing BMP file... doesn't work correctly
    By tin in forum Game Programming
    Replies: 3
    Last Post: 12-28-2005, 04:40 AM
  4. Program doesn't work as it should.
    By Cris987 in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2003, 01:06 PM
  5. linked list doesn't work correctly
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-12-2002, 11:32 AM

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