K & R: Summary 1

Source: Internet
Author: User

1. Delete all squeeze (char s [], int c) characters in string S)

void squeeze_test(char str[],int c)         {                int i,j;                for (i=j=0;str[i] !='\0'; i++)                     if (str[i] != c)                            str[j++]=str[i];                    str[j]='\0';         }

2. The standard library function strcat (S, T) connects string T to the end of string S-assuming s has enough space to save

void strcat(char s[], char t[])
{   int i, j;   i=j=0;   while(s[i] !='\0')   i++;   while((s[i++] = t[j++]) != '\0')   ;}

3. The getbits (x, P, n) function returns the n-bits from the right starting from the P-bits on the Right of X.

unsigned getbits(unsigned x, int p, int n){return (x >> (p+1)) & ~(~0 << n);}

4. The C language is used to determine the big end and small end.

#include<stdio.h>int check(){union check{int i;char ch;}c;c.i  =1;return (c.ch == 1);}int main(){   int ret;   ret = check();   if(ret == 0)   {   printf("Big\n");   }   else   {    printf("little\n");   }   return 0;}

5. Differences between structures and communities

A, struct, and union are composed of multiple members of different data types, but at any time, union only stores one selected member, all the members of struct exist. In struct, each member occupies its own memory space and they exist at the same time. The total length of a struct variable is equal to the sum of all member lengths. In union, all Members cannot occupy their memory space at the same time, and they cannot exist at the same time. The length of the Union variable is equal to the length of the longest member.

B. assigning values to different members of the union operation will be rewritten to other members. The original value of the union operation does not exist, but it does not affect the assignment of different members of the struct operation.

 

6. Calculate the computer wordlength worldlength () of the running program ()

int worldlength(void){   int i;   unsigned v = (unsigned)~0;      for (i = 1; (v = v >> 1) >0); i++)        ;   return i ;}

7. The bitcount () function counts the binary digits with a value of 1 in X.

int bitcount(unsigned x){  int b;  for (b = 0; x !=0; x>>= 1)     if (x & 01)        b++;  return b;}

8. Half-fold search (Binary Search)

/* Binsearch function, V array is an array in ascending order */INT binsearch (int x, int V [], int N) {int low, high, mid; low = 0; high = n-1; while (low <= high) {mid = (low + high)/2; If (x <V [Mid]) high = mid-1; else if low = Mid + 1; else return mid;} return-1 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.