2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
int main()
{
    system("cls");
    int i1,n;
    scanf("%d\n",&n);
    for(i1=0;i1<n;i1++)
    {
        char *s;
        s=(char *)malloc(sizeof(char)*20);
        gets(s);
        int l=strlen(s);
        int l1=l;
        int i,j;
        for(i=0;i<l;i++)
        {
            if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='O'||s[i]=='I'||s[i]=='U')
            {
                for(j=l1-1;j>=0;j--)
                {
                    if(s[j]=='a'||s[j]=='e'||s[j]=='i'||s[j]=='o'||s[j]=='u'||s[j]=='A'||s[j]=='E'||s[j]=='O'||s[j]=='I'||s[j]=='U')
                    {
                        printf("%c",s[j]);
                        l1=j;
                        break;
                    }
                }
            } 
            else
            {
            printf("%c",s[i]);
            }
        }
        printf("\n");
        free(s);
    }
    getch();
    return 0;
}

it's a program to reverse the order of the vowels of a string (education -> odicatuen). In the image below the left one is input file & right one is output file. You can see there is an upper arrow at the beginning

left one is input file & right one is output file

There is no bug in the program. It works fine. I have an input text file & I'm saving my output in an output text file via command prompt. I am getting an unexpected "upper arrow character" at the beginning of my output file

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 8
    "there is no bug in the program" - so why do you post here? – too honest for this site Mar 25 '17 at 16:15
  • 7
    Don't use `gets`, don't ever use it. It is a dangerous function that will sooner or later lead to major problems if you keep on using it. Besides, it's was deprecated in the C99 standard and removed completely in the latest C11 standard. – Some programmer dude Mar 25 '17 at 16:15
  • 2
    Also, a debugger is a very useful tool, one that you really need to learn how to use if you're in any way serious about being a programmer, even on a hobbyist level. – Some programmer dude Mar 25 '17 at 16:16
  • Actually i wanted to say it works fine when input is taken from command prompt not from an text file !! @Olaf – Arijit Karali Mar 25 '17 at 16:20
  • 1
    [don't cast malloc in C](http://stackoverflow.com/q/605845/995714). [Why is the gets function so dangerous that it should not be used?](http://stackoverflow.com/q/1694036/995714) – phuclv Mar 25 '17 at 16:27
  • Not sure, but try removing `system("cls");` and run the program. – Spikatrix Mar 25 '17 at 16:32
  • On Linux (with MS specific stuff commented out), it runs as expected with no complaints from Valgrind. Under MSVC, there's the extra byte at the start. – dbush Mar 25 '17 at 16:44
  • 1
    I'm voting to close this question as off-topic because there is no bug in the program and it is working fine. – n. m. could be an AI Mar 25 '17 at 17:31

2 Answers2

3

The problem is caused by the call to system("cls");

The cls command clears the console screen. This is done by printing a form feed character (ASCII value 12) to stdout. A command prompt interprets this character as clearing the screen. But when you redirect output to a file, this character becomes part of the output. So the up-arrow character you're seeing is how ASCII code 12 is being displayed in Notepad.

Remove the call to system("cls"); and you won't get the extra output in the file.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

You state: There is no bug in the program. It works fine

however, this copy of your posted code, shows the bugs:

Some suggestions:

  1. only one statement per line and (at most) one variable declaration per statement.
  2. separate code blocks (for, if, else, while, do...while, switch, case, default via a single blank line
  3. use appropriate horizontal spacing for readability
  4. variable and parameter names should indicate 'content' or 'usage' (or better, both)
  5. Consistently indent the code. indent after every opening brace '{'. unindent before every closing brace '}'.
  6. your compiler should have told you about 'gets()' if it did not, then get a modern compiler and/or turn on the warnings.

and now your code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>           // <== this is not portable, strongly suggest using the C standard library functionality

int main()                   // <-- suggest: 'int main( void )'
{
    system("cls");           // <-- this is not portable, 
                             //     suggest using the ansi terminal escape sequences
    int i1,n;                // <-- better written as
                             //     'int il;'
                             //     'int n;'

    scanf("%d\n",&n);        // <--This will (usually) fail due to the '\n' in the format string
                             // <-- when calling any of the 'scanf()' family of functions
                             //     always check the returned value (not the parameter value)
                             //     to assure the operation was successful
                             //     I.E.
                             //     'if( 1 != scanf( "%d", &n ) )'
                             //     '{'
                             //          'perror( "scanf failed" );'
                             //          'exit( EXIT_FAILURE );'
                             //     '}'
    for(i1=0;i1<n;i1++)      // <-- for readability suggest:
                             //     'for( il=0; il<n; il++ )'
    {
        char *s;
        s=(char *)malloc(sizeof(char)*20);
                             // <-- when calling any of the heap allocation functions (malloc, calloc, realloc)
                             //     1) do not cast the returned value.  The returned value has type 'void*'
                             //        which can be assigned to any other pointer
                             //        casting just clutters the code
                             //     2) always check (!=NULL) the returned value to assure the operation was successful.
                             //     3) the expression 'sizeof(char)' is defined in the standard as 1
                             //        in the parameter to any of the heap allocation functions,
                             //        multiplying by 1 has no effect and just clutters the code                           
                             //     suggest:
                             //     'if( NULL == (s = malloc(20) ) )'
                             //     '{'
                             //         'perror( "malloc failed" )'
                             //         'exit( EXIT_FAILURE );'
                             //     '}'
        gets(s);             // <-- the function 'gets()' has been depreciated for years and
                             //     completely eliminated in the latest C standard
                             //     suggest:
                             //     'if( ! fgets( s, 20, stdin ) )'
                             //     '{'
                             //         'perror( "fgets failed" )'
                             //         'free( s );   // cleanup'
                             //         'exit( EXIT_FAILURE );'
                             //     '}'
        int l=strlen(s);     // <-- 'strlen()' returns a 'size_t' not an 'int'
        int l1=l;            // <-- assigning an 'size_t' to an 'int' is problematic

        int i,j;             // <-- note: earlier comments about variable declarations

        for(i=0;i<l;i++)     // <-- note: earlier comments about readability and horizontal spacing
        {
            if(s[i]=='a'     // <-- code lines should honor the width of the printed page (80 or less characters)
             ||s[i]=='e'     //     what about 'y'/'Y' is sometimes a vowel
             ||s[i]=='i'     // <-- you should learn about 'toupper()' and 'tolower()'
             ||s[i]=='o'
             ||s[i]=='u'
             ||s[i]=='A'
             ||s[i]=='E'
             ||s[i]=='O'
             ||s[i]=='I'
             ||s[i]=='U')
            {

                for(j=l1-1;j>=0;j--)  // <-- note earlier comments about readability and horizontal spacing
                {
                    if(s[j]=='a'
                     ||s[j]=='e'
                     ||s[j]=='i'
                     ||s[j]=='o'
                     ||s[j]=='u'
                     ||s[j]=='A'
                     ||s[j]=='E'
                     ||s[j]=='O'
                     ||s[j]=='I'
                     ||s[j]=='U')
                    {
                        printf("%c",s[j]);
                        l1=j;
                        break;
                    }
                }
            }
                                      // <-- note earlier comment about readability
            else
            {
            printf("%c",s[i]);        // consistently indent the code
            }
        }

        printf("\n");
        free(s);
    }
                                      // <-- note: earlier comment about readability
    getch();                          // <-- this line is not portable
                                      //     suggest:
                                      //     'int ch;'
                                      //     'while( (ch = getchar()) != EOF && '\n' != ch );'
                                      //     'getchar()'
    return 0;                         // from 'main()' if returned value is 0 then this line not needed
} // end function: main

Note: 'strlen()' gives the offset to the trailing NUL char so that NUL char is being printed.

Note: the output is (per your question) being re-directed to a file. so no cursor manipulations are allowed. The call to 'system( "cls" )' is a cursor manipulation and since this is not being output to a terminal handler, it is saved in the file. That is why your file contains the unexpected 'up arrow' character.

user3629249
  • 16,402
  • 1
  • 16
  • 17