Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 2-2
- ==========
- #include <stdio.h>
- #define MAX_STRING_LENGTH 100
- int main(void)
- {
- /*
- for (i = 0; i < lim-1 && (c=getchar()) != '\n' && c != EOF; ++i)
- s[i] = c;
- */
- int i = 0,
- lim = MAX_STRING_LENGTH,
- c;
- char s[MAX_STRING_LENGTH];
- while (i < (lim - 1))
- {
- c = getchar();
- if (c == EOF)
- break;
- else if (c == '\n')
- break;
- s[i++] = c;
- }
- s[i] = '\0'; /* terminate the string */
- return 0;
- }
- 2-3
- ==========
- #include <stdio.h>
- #include <math.h>
- int upperToLower(int character);
- int main(void){
- char hexString[10];
- int i, j, c, input, integer;
- char hexadecimals[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
- '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
- i = 0;
- while ((c = getchar()) != EOF)
- {
- if (c >= 'A' & c <= 'Z')
- {
- c = upperToLower(c);
- }
- hexString[i] = c;
- ++i;
- }
- hexString[i] = '\0';
- integer = 0;
- for (j = i - 1; j >= 0; --j)
- {
- if(hexString[j] >= 'a' && hexString[j] <= 'z')
- {
- for(int k = 0; k < 16; ++k)
- {
- if(hexString[j] == hexadecimals[k])
- {
- integer = integer + k * pow(16, i-1-j);
- }
- }
- }
- else
- integer = integer + (hexString[j] - '0') * pow(16, i-1-j);
- }
- printf("\nThe integer value of the hexadecimal string %s is ", hexString);
- printf("d.\n", integer);
- }
- int upperToLower(int input){
- int lower;
- lower = input + 'a' - 'A';
- return lower;
- }
- 2-4
- ==========
- 2-5
- ==========
- int any(char s1[], char s2[])
- {
- int i;
- int j;
- int pos;
- pos = -1;
- for(i = 0; pos == -1 && s1[i] != '\0'; i++)
- {
- for(j = 0; pos == -1 && s2[j] != '\0'; j++)
- {
- if(s2[j] == s1[i])
- {
- pos = i;
- }
- }
- }
- return pos;
- }
- 2-6
- ==========
- 2-7
- ==========
- 2-8
- ==========
- 2-9
- ==========
- int bitcount(unsigned x) {
- int b=0;
- while (x != 0) {
- x &= (x-1);
- b++;
- }
- return b;
- }
- int main() {
- printf("%d\n", bitcount(25));
- }
- 2-10
- ==========
- #include <stdio.h>
- int lower(int c) {
- return (c >= 'A' && c <= 'Z') ? c + 'a' - 'A' : c;
- }
- 3-1
- ==========
- int binsearch2(int x, int v[], int n)
- {
- int low, high, mid;
- low = -1;
- high = n;
- while (low + 1 < high) {
- mid = (low + high) / 2;
- if (v[mid] < x)
- low = mid;
- else
- high = mid;
- }
- if (high == n || v[high] != x)
- return -1;
- else
- return high;
- }
- 3-2
- ==========
- void escape(char * s, char * t) {
- int i, j;
- i = j = 0;
- while ( t[i] ) {
- /* Translate the special character, if we have one */
- switch( t[i] ) {
- case '\n':
- s[j++] = '\\';
- s[j] = 'n';
- break;
- case '\t':
- s[j++] = '\\';
- s[j] = 't';
- break;
- case '\a':
- s[j++] = '\\';
- s[j] = 'a';
- break;
- case '\b':
- s[j++] = '\\';
- s[j] = 'b';
- break;
- case '\f':
- s[j++] = '\\';
- s[j] = 'f';
- break;
- case '\r':
- s[j++] = '\\';
- s[j] = 'r';
- break;
- case '\v':
- s[j++] = '\\';
- s[j] = 'v';
- break;
- case '\\':
- s[j++] = '\\';
- s[j] = '\\';
- break;
- case '\"':
- s[j++] = '\\';
- s[j] = '\"';
- break;
- default:
- /* This is not a special character, so just copy it */
- s[j] = t[i];
- break;
- }
- ++i;
- ++j;
- }
- s[j] = t[i]; /* Don't forget the null character */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement