今天记录几个简短而有用的程序
*******倒序转换*********
void reverse(char s[])
{
int i,j;
char temp;
i=0;
while(s[i]!='\0') //fine the end of the string
++i;
--i;
if(s[i]=='\n')
--i;
j=0; //begining of a new line
while (j<i){ //reverse a line ,cut from the middle
temp=s[j];
s[j]=s[i];
s[i]=temp;
--i;
++j;
}
}
*******输入一行字符*********
int getline(char s[],int lim) //read each line and store it
{
int c,i;
for(i=0;i<lim-1&&(c=getchar())!=EOF&&c!='\n';++i)
s[i]=c;
if(c=='\n')
{
s[i]=c;
++i;
}
s[i]='\0';
return i;
}
**********数组的值比较与删除********************************
// squeeze : delete each char in s1 which is in s2
void squeeze(char s1[],char s2[])
{
int i,j,k;
for(i=k=0;s1[i]!='\0';i++)
{
for(j=0;s2[j]!='\0'&&s2[j]!=s1[i];j++)
;
if(s2[j]=='\0') // enf of the string ;-no match
s1[k++]=s1[i];
}
s1[k]='\0';
}
//如果在原S1[]上操作整个数组,删除元素会产生移位。所以应另加一个数组来存贮不等于S2[]上的值。
//逐个比较在S2[]上的元素,如果都不相同,意味着S2[]到了终点,S2[J]=‘\0’,以此作为标志。
**************************最基本的知识 进制的表示方法******************
C语言的各进制的表示方法
int a= 123; //十进制
int a=0123;//八进制
int a=0x123;//十六进制
************************数出数值中1的位数****************
int bitcount( unsigned x )
{ int cout;
for(cout=0;x!=0;cout++)
cout&=(cout-1); //
}