Skip to content

Instantly share code, notes, and snippets.

@tomcha
tomcha / 1-24.c
Created April 7, 2015 10:27
1-24
#include <stdio.h>
#define MAXSTRINGS 10000
// 1-23に同じく、catとパイプでソースを受け取る
int main(){
int c;
char str[MAXSTRINGS];
int i = 0, j;
int n = 1;
int qflg =0; // normal -> 0, sq -> 1, dq -> 2
int eflg = 0; // normal -> 0, // -> 1, /* -> 2
@tomcha
tomcha / 2-2.c
Created April 18, 2015 11:08
2-2
#include <stdio.h>
#define MAXCHARS 1000
void getlined(char s[], int lim);
int main(){
char s[MAXCHARS];
getlined(s, MAXCHARS);
printf("%s\n", s);
}
@tomcha
tomcha / 2-3.c
Created April 18, 2015 11:08
2-3
[tomcha]$ cat 2-3.c
#include <stdio.h>
int htoi(char s[]);
int main(){
printf("%d\n", htoi("0x1a\n"));
}
int htoi(char s[]){
int result = 0;
@tomcha
tomcha / 2-4.c
Created April 18, 2015 11:09
2-4
#include <stdio.h>
void squeeze(char s1[], char s2[]);
int main(){
char s1[11] = "abcdefffc\n";
char s2[4] = "ce\n";
squeeze(s1, s2);
printf("%s\n", s1);
}
@tomcha
tomcha / 2-5.c
Created April 18, 2015 11:10
2-5
#include <stdio.h>
int any(char s1[], char s2[]);
int main(){
//"abcdefg"
//"cde" -> 2
//"cdf" -> -1
printf("1:%d\n", any("abcdef", "cde"));
printf("2:%d\n", any("abcdef", "cdf"));
@tomcha
tomcha / 2-6.c
Created April 18, 2015 11:10
2-6
#include <stdio.h>
int unsigned setbits(unsigned x, int p, int n, unsigned y);
int main(){
int x = 0b001110;
int y = 0b0000000;
int val = setbits(x, 3, 3, y);
printf("%d\n", val);
// answer 0b0000111 -> 7;
@tomcha
tomcha / 2-7.c
Created April 18, 2015 11:11
2-7
#include <stdio.h>
int invert(unsigned x, int p, int n);
int main(){
int x = 0b00011100;
int y = 0b00010000;
// x -> 0b00000000 = 0
// y -> 0b00001100 = 12
@tomcha
tomcha / 2-8.c
Created April 18, 2015 11:11
2-8
#include <stdio.h>
int rightrot(unsigned x, int n);
int main(){
int x = 0b111010;
printf("%d\n",rightrot(x, 2));
// 111010 -> 101110 = 46
}
int rightrot(unsigned x, int n){
@tomcha
tomcha / 2-9.c
Created April 22, 2015 10:02
2-9
#include <stdio.h>
int bitcount_kai(unsigned x);
int main(){
printf("%d\n", bitcount_kai(9));
}
int bitcount_kai(unsigned x){
int b = 0;
@tomcha
tomcha / 2-10.c
Created April 22, 2015 10:03
2-10
#include <stdio.h>
int lower(int c);
int main(){
printf("%c\n", lower('T'));
printf("%c\n", lower('t'));
printf("%c\n", lower('Z'));
printf("%c\n", lower('z'));
}