# Thread: Repeat vowel Problem?

1. ## Repeat vowel Problem?

Repeat the vowel Problem 3 (0 / 0)

Write a program that will repeat k times each single occurrence of a vowel in the input file "sp.txt" to a new file "output.txt". The first line of the input file contains only the parameter k. The first line (containing the parameter k) should not to be written in the output file.

hi guys. I wrote the code but i cant figure out something. i read the file, i found the vowels but then i cant print them. can anyone help me ?

Code:
#include <stdio.h>#include <string.h>
#include <ctype.h>
#define MAX 100

int checkvowel(char c)
{
if(c=='A' || c=='a' || c=='E' || c=='e' || c=='o' || c=='O' || c=='u' || c=='U' || c=='i' || c=='I')
return 1;
else
return 0;
}

void wtf() {
FILE *f = fopen("sp.txt", "w");
char c;
while((c = getchar()) != EOF) {
fputc(c, f);
}
fclose(f);
}
void rff() {
FILE *f = fopen("output.txt", "r");
char c;
while((c = fgetc(f)) != EOF) {
putchar(c);
}
fclose(f);
}

int main() {
wtf();
int k;
char b[100];
scanf("%d",&k);
FILE *f1;
FILE *f2;
f1=fopen("output.txt","r");
if(f1==NULL)
{
printf("WE CANT OPEN THE FILE");
return -1;
}

char c[100];
int z=0,i;
while(fgets(c,100,f1))
{
for(i=0; i<strlen(c); i++)
{
if(isalpha(c[i]))
{
if(checkvowel(c[i]))
{
for(i=0; i<k; i++)
{
b[z]=c[i];
z++;
}
}
if(!(checkvowel(c[i])))
{
b[z]=c[i];
z++;
}
}
else
b[z]=c[i];
z++;
}
}

fclose(f1);

f2=fopen("sp.txt","w");
if(f1==NULL)
{
printf("WE CANT OPEN THE FILE");
return -1;
}

rff();
return 0;

}

2. Originally Posted by Khwarizm
i found the vowels but then i cant print them. can anyone help me ?

A couple of suggestions. 1. Write pseudocode of how you intent to solve the problem. 2. When you post code in the forum that you want people to read, make sure it is formatted nicely (blocks are indented, etc.).

Pseudocode should look something like this

Code:
Open sp.txt
Open output.txt
Read a line from sp.txt and save the number in k
While there are more characters in sp.txt:
Read the next character c from sp.txt
If c is a vowel:
Output c k times to output.txt



3. The logic of your code escapes me - probably because you've been adding code by guesswork and relying on prayer to get it working.

An obvious problem, since you have said that your program is supposed to be writing to output.txt, is that output.txt is only ever opened for reading, and data is only read from it.

4. Originally Posted by grumpy
The logic of your code escapes me - probably because you've been adding code by guesswork and relying on prayer to get it working.

An obvious problem, since you have said that your program is supposed to be writing to output.txt, is that output.txt is only ever opened for reading, and data is only read from it.
i changed a little but again its doesnt work. its compiles correctly but doesnt print anything.

Code:
#include <stdio.h>#include <string.h>
#include <ctype.h>
#define MAX 100

int check_vowel ( char c) {
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

void wtf() {
FILE *f = fopen("output.txt", "w");
char c;
while((c = getchar()) != EOF) {
fputc(c, f);
}
fclose(f);
}
void rff() {
FILE *f = fopen("sp.txt", "r");
char c;
while((c = fgetc(f)) != EOF) {
putchar(c);
}
fclose(f);
}

int main() {
wtf();
FILE *f = fopen("sp.txt", "r");
FILE *f2= fopen("output.txt","w");
char c;
int k, i;
char line[100];
fgets(line,100,f);
scanf("%d",&k);

while((c = fgetc(f)) != EOF) {
if(isalpha(c)){
if(check_vowel(tolower(c))) {
for (i=0; i<=k; i++)
fprintf(f2,"%c",c);
}
else
fprintf(f2,"%c",c);

}
else
fprintf(f2,"%c",c);
}

fclose(f);
fclose(f2);

rff();
return 0;

}

5. You describe your requirement as reading from sp.txt and writing to output.txt.

Instead, what it does is

1) read input from stdin, writes that to output.txt, and closes output.txt
2) re-opens output.txt for writing (which deletes everything written to that file previously).
3) reads a line from sp.txt, an integer from stdin, and then reads a bunch of input from sp.txt while writing to output.txt
4) closes the files.
5) re-opens sp.txt and writes to stdout.

As I mentioned previously, your code writes as if you are just adding statements by guesswork, and hoping they will do what you want. Stop guessing, and be systematic.

6. You've got these bits in several places:

Code:
char c;
while((c = getchar()) != EOF) {
Code:
char c;
...
while((c = fgetc(f)) != EOF) {
The fgetc/getchar functions return an int, not a char. Sometimes this fact can be ignored but not in scenarios where you are comparing against EOF.

If you want to read why, then read FAQ > Definition of EOF and how to use it effectively and try to pay particular attention to the later half of that page starting with the section labeled "So what does all this mean to me as a programmer?"

7. thanks for advice guys i corrected and i works fine now.

Popular pages Recent additions