I need help with a for loop in my string processing code.
I can split the input into sentences that end with a period.
I can split the sentence into individual words.
I can remove the null strtok puts on and so I can use a for loop.
Why I want to use a for loop is each loop processes a single sentence.
When the sentence is processed in becomes individual words,
and then a percentage that's written into a file.
After the sentence is written to file as a percentage I want to loop
back and get another sentence, until the source is empty.
Here's my unanswered questions I need to know to solve my problem:
I plan to use a for loop.
1) I don't know if I'm looping through b or the single sentence in pch?
2) I don't know how to increment the for loop, do I use a int and a new variable?.
3) I don't know how to break out of the for loop. One the loop is finished a if statement tests to see if the count is \0,
but I'm not sure on the specifics.
I was going to try to use Adaks code, but after reading about strtok I found the problem was it makes the original string have NULL.
My technique of using strpbrk fixes that.
I'm semi-sure that my solution of using a for loop can let me process individual sentences, but I'm not sure on how to do it. Because Adak may want to see what I did with his code I will post that after I post the code I'm working and and made the thread for.
The code I need help with:
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 alphabet[] = "abcdefghijklmnopqrstuvwxyz";
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, ".");
/* I tokenize the input string into individual sentences */
while(pch != NULL)
{
pch = strtok (NULL, ".");
}
/* pch is NULL, so I find alphabet in b and pass that into pch */
pch = strpbrk(b, alphabet);
/* I tokenize the input string into individual words */
pch = strtok (b, " ");
/* numchar counts the numbers of characters in the pch string */
numchar = countchar (pch);
/* The while loop gives value to five, six, seven, eight, which is used for the percentage calculation */
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, " ");
}
/* pch is NULL, so I find alphabet in b and pass that into pch */
pch = strpbrk(b, alphabet);
/* pch is backwards, so I use rev to reverse it back */
/*printf("%s uncomment to test to see if the string is backwards", pch);*/
reverse = rev(pch);
/* 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);
}
Adak's code I tweaked. I'm not posting for help with this code:
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main (void) {
int character, J, len;
char string[]= {"There. is 10, 9daysaway! 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((isalnum(string[character]))||(ispunct(string[character])))
{
if(string[character+1]==' ' || string[character+1]=='\n'|| string[character+1]=='.'|| string[character+1]==','|| string[character+1]=='!')
{
putchar(string[character]);
putchar('\n');
}
if(J==0)
putchar(string[character]);
else
strlen(string);
++J;
}
else
{
J=0;
}
}
printf("\n\n");
getch();
return 0;
}
I'm stuck, so if you don't answer me outright, which I know you people don't like to do, then give me some code that will get me going again because right now I'm stuck. :D