#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;
}
这是一个扭转字符串元音(education -> odicatuen
)顺序的程序。在左下方的图像中输入文件&amp;右边一个是输出文件。你可以看到开头有一个上箭头
程序中没有错误。它工作正常。我有一个输入文本文件&amp;我通过命令提示符将输出保存在输出文本文件中。我在输出文件的开头有一个意外的“上箭头字符”
答案 0 :(得分:3)
问题是由对system("cls");
cls
命令清除控制台屏幕。这是通过将换页字符(ASCII值12)打印到stdout
来完成的。命令提示符将此字符解释为清除屏幕。但是当您将输出重定向到文件时,此字符将成为输出的一部分。因此,您所看到的向上箭头字符是如何在记事本中显示ASCII代码。
删除对system("cls");
的调用,但您不会在文件中获得额外的输出。
答案 1 :(得分:0)
你声明:程序中没有错误。它工作正常
但是,您发布的代码的副本显示了错误:
一些建议:
现在你的代码:
#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
注意:&#39; strlen()&#39;给尾随NUL字符赋予偏移量 所以正在打印NUL char。
注意:输出(根据您的问题)被重定向到文件。所以不允许游标操作。呼叫系统(&#34; cls&#34;)&#39;是一个游标操作,因为它没有输出到终端处理程序,它被保存在文件中。这就是为什么您的文件包含意外的向上箭头&#39;字符。