# C Programming Language (Second Edition) 2-8 ☆

2010-07-15  来源：本站原创  分类：CPP  人气：182

2-8 Write a function rightsort (x, n), the function returns the x n-bit cyclic right shift the value obtained.

```#include<stdio.h>

unsigned rightrot(unsigned x,unsigned n);

int main(){
unsigned x=5;
unsigned n=1;

unsigned result;
result = rightrot(x,n);
printf("%d\n",result);
return 0;
}

unsigned rightrot(unsigned x,unsigned n)
{
while(n>0)
{
if((x&1)==1)
x= (x>>1) | ~(~0U >> 1);
else
x=x>>1;
n--;
}
return x;
}
```

Below this function also, the note ~ (~ 0U>> 1) and ~ 0U <<(size-1) effect is the same as learning the use of size

```#include <limits.h>
unsigned rightrot(unsigned x, unsigned n)
{
int size= sizeof(x) * CHAR_BIT;
while (n > 0) {
if ((x & 1) == 1)
x = (x >> 1) | (~0U << (size-1));
else
x = (x >> 1);
n--;
}
return x;
}
```

• C Programming Language (Second Edition) 1-8 2010-07-04

//1-8 Output the input character spaces, tabs , The number of line breaks #include<stdio.h> void main(){ int c,space,table,enter; space=0; table=0; enter=0; while((c=getchar())!=EOF){ if(c==' ')// Spaces of ASCII to 32 space++; if(c=='\t')// Tab cha

• C Programming Language (Second Edition) 1-9 2010-07-04

// Writing a will enter the copy to output of the program, with multiple consecutive spaces are replaced with a space #include<stdio.h> void main(){ int c; int flag=0; while((c=getchar())!=EOF){ if(c!=' '){ flag=0; putchar(c); } if(c==' '){ if(flag=

• C Programming Language (Second Edition) 1-10 2010-07-04

// Writing a will enter the copy to output of the program, and will replace one of the tabs \t, The fallback character to be replaced by \b, The backslash to be replaced by \\. #include<stdio.h> void main(){ int c; while((c=getchar())!=EOF){ if(c=='

• C Programming Language (Second Edition) 1-13 2010-07-05

Write a program that prints the input word length of the histogram. Horizontal direction is easier, the vertical direction is harder Horizontal: #include<stdio.h> //#define aaa 10; void main(){ //int charArray[aaa];// Recording every word counts the

• C Programming Language (Second Edition) 1-14 2010-07-05

Write a program that prints the input frequency histogram of each character #include<stdio.h> void main(){ int letters[26]; for(int i=0;i<26;i++) letters[i]=0; int c; //a c=getchar(); int charA=65;//A ASCII code 65,a ASCII to 97 int chara=97; whi

• C Programming Language (Second Edition) 1-16 2010-07-11

1-16 modify the procedure to print the longest line of text in the main program main, so that you can print any length of input line length, and as much as possible to print text. #include <stdio.h> #define MAXLINE 10 int getline(char line[], int ma

• C Programming Language (Second Edition) 1-17 2010-07-11

1-17 Write a program longer than 80 characters to print all input lines #include<stdio.h> #define MINLENGTH 80 int readbuff(char* buff) { size_t i=0; int c; //c=getchar(); while(i<MINLENGTH) { c=getchar(); if(c==EOF) return -1; if(c=='\n') return

• C Programming Language (Second Edition) 1-19 2010-07-12

1-19 write function reserse (s), the string s in the order reversed. Write a program using this function, each time an input line in reverse order of characters. #include<stdio.h> #define LENGTH 5 void getline(char s[]) { int c, i; for(i = 0; ((c =

• C Programming Language (Second Edition) 2-1 2010-07-13

2-1 Write a program to determine the signed and unsigned by the limited char, short, int and long types of variable range. Print the standard header file with the corresponding value: #include <stdio.h> #include <limits.h> int main(){ printf(&

• C Programming Language (Second Edition) 2-3 2010-07-14

2-3 write function htoi (s), to the string of hexadecimal digits (including the optional prefix 0x or 0X) into which are equivalent to the integer value. String contains the number of allowed includes: 0 ~ 9, a ~ f, A ~ Z #include <stdio.h> #include

• C Programming Language (Second Edition) 2-4 2010-07-14

2-4 write function squeeze (s1, s2), the string s1 with string s2 matches characters in the character delete #include<stdio.h> #define max 100 void squeeze(char* leftStrig,char* rightString); int main(){ char left[100]="shizhendema"; char

• C Programming Language (Second Edition) 2-5 2010-07-14

2-5 write the function any (s1, s2), s2 in the string any character in the string s2 occurs in the first place as a result of return, if s1 has not included the character in s2, returning -1 #include<stdio.h> #define max 100 void rlString(char* s1,c

• C Programming Language (Second Edition) 2-6 2010-07-14

2-6 Write a function setbits (x, p, n, y), x the function returns the result of the following values: the x in the position from the beginning of the n-p (binary) bit is set to y of the most the right side of n-bit value, x remains the same the rest

• C Programming Language (Second Edition) 2-7 ☆ 2010-07-15

2-7 Write a function invert (x, p, n), x the function returns the result of the following values in the x position from the beginning of the n-p (binary) bit demand against (that is, a change into 0,0 into 1), x the rest of you remains the same. #inc

• C Programming Language (Second Edition) 2-8 ☆ 2010-07-15

2-8 Write a function rightsort (x, n), the function returns the x n-bit cyclic right shift the value obtained. #include<stdio.h> unsigned rightrot(unsigned x,unsigned n); int main(){ unsigned x=5; unsigned n=1; unsigned result; result = rightrot(x,n

• C Programming Language (Second Edition) 2-9 2010-07-15

2-9 bitcount () function: statistics x in the binary digit value of 1 #include <stdio.h> int bitcount(unsigned x){ int b; for(b=0;x!=0;x>>=1) if(x&1) b++; return b; } int main () { unsigned x=10;// X declared as unsigned integer number in

• C Programming Language (Second Edition) 3-1 2010-07-15

3-1 binary search, two versions int binsearch(int x, int v[], int n) { int low, mid, high; low = 0; high = n - 1; while ( low <= high ) { mid = (low+high) / 2; if ( x < v[mid] ) high = mid - 1; else if ( x > v[mid] ) low = mid + 1; else return mi

• C Programming Language (Second Edition) 3-2 2010-07-15

3-2 Write a function escape (s, t), the string copied to the string t in s, and line breaks in the replication process will, tabs and other characters were not visible into \ n, \ t, etc. shows the corresponding escape characters. Then write a function of

• C Programming Language (Second Edition) 3-3 2010-07-16

3-3 write function expand (s1, s2), s1 string of similar sort of shorthand with az in the string s2 in the expansion symbol into an equivalent complete list abc ... xyz. The function can handle uppercase and lowercase letters and numbers, and can dea

• C Programming Language (Second Edition) 4-1 2010-07-19

4-1 write function strrindex (s, t), which returns the string t in s the most on the right to appear. If s does not contain t, then return -1 #include<stdio.h> #define MAXLINE 100 int getline(char line[], int max); int strrindex(char s[], char t[]);

1
2
3
4
5
6
7
8
9
10