Department of Engineering

Next: Exercises 3 Up: Sample answers to exercises Previous: Exercises 1   Contents

## Exercises 2

• ```int mystrcmp(const char *s1, const char *s2){
while(*s1++=*s2++)
;
}
```

• ```int ccase;
if (skew >= 0){
ccase = copy_right + function;
}
else{
bptr = bptr + chunk_bytes;
ccase = copy_left + function;
}
```
• ```char *strchr(const char* str, int c)
{
while(*str !='\0'){
if (*str == c)
return str;
else
str++;
}
return NULL;
}
```
• ```#include <stdio.h>
#include <stdlib.h>

char * get_string(char str[])
{
printf("Input a string: ");
return gets(str);
}

main(){
int degrees;
char scale;
int return_value;
char string[1023];
while(1){
printf("Please type in a string like 20C or 15F\n");
printf("Use control-C to quit\n");
get_string(string);
return_value = sscanf(string,"%d%c",&degrees, &scale);
if (return_value != 2){
printf("There's a mistake in your input. Try again.\n");
continue;
}
if (( scale == 'f')|| (scale == 'F'))
printf("%s is %dC\n",string,((degrees-32)*5)/9);
else
if (( scale == 'c')|| (scale == 'C'))
printf("%s is %dF\n",string, (degrees*9)/5+32);
else{
printf("Unable to determine whether you typed C or F\n");
printf("Try again.\n");
}
}
}
```

Tim Love 2010-04-27