You state: There is no bug in the program. It works fine
however, this copy of your posted code, shows the bugs:
Some suggestions:
- only one statement per line and (at most) one variable declaration per statement.
- separate code blocks (for, if, else, while, do...while, switch, case, default via a single blank line
- use appropriate horizontal spacing for readability
- variable and parameter names should indicate 'content' or 'usage' (or better, both)
- Consistently indent the code. indent after every opening brace '{'. unindent before every closing brace '}'.
- 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.