Thread: Repeat vowel Problem?

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    6

    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. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by Khwarizm View Post
    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. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    Quote Originally Posted by grumpy View Post
    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. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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?"
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Nov 2013
    Posts
    6
    thanks for advice guys i corrected and i works fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 01-11-2014, 03:48 PM
  2. WAP to remove vowel string using Pointer and Function
    By Pallav Mahamana in forum C Programming
    Replies: 6
    Last Post: 01-28-2013, 02:26 PM
  3. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  4. Repeat condition problem
    By kumar14878 in forum C Programming
    Replies: 3
    Last Post: 05-04-2005, 07:43 AM
  5. repeat
    By linuxdude in forum C Programming
    Replies: 4
    Last Post: 03-28-2003, 09:40 AM