textbook code below, given the code might have typo:
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main(void){
printf("%s\\n%s%s\\n%s%s\\n\\n","According to isdigit: ",
isdigit('8')? "8 is a":"8 is not a ","digit",
isdigit('#')? "# is a":"# is not a ","digit");
// printf("%s\\n%s%s\\n%s%s\\n%s%s\\n%s%s\\n\\n","According to isdigit: ",
// isalpha('A')? "A is a":"8 is not a ","digit",
// isdigit('#')? "b is a":"# is not a ","digit");
return 0;
}
8.6
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main(void){
const char *string = "51.2% are admitted";
char *stringPtr;
double d=strtod(string, &stringPtr);
printf("The string \\"%s\\" is converted to the \\n", string);
printf("double value %.2f and the string \\"%s\\"\\n",d,stringPtr);
return 0;
}
8.7
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char *string = "-1234567abc"; // initialize string pointer
char *remainderPtr; // create char pointer
long x = strtol(string, &remainderPtr, 0);
printf("%s%s\\n%s%ld\\n%s\\"%s\\"\\n%s%ld\\n",
"The original string is ", string,
"The converted value is ", x,
"The remainder of the original string is ", remainderPtr,
"The converted value plus 567 is ", x + 567);
return 0;
}
8.8
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char *string = "1234567abc"; // initialize string pointer
char *remainderPtr; // create char pointer
unsigned long int x = strtoul(string, &remainderPtr, 0);
printf("%s\\"%s\\"\\n%s%lu\\n%s\\"%s\\"\\n%s%lu\\n",
"The original string is ", string,
"The converted value is ", x,
"The remainder of the original string is ", remainderPtr,
"The converted value minus 567 is ", x - 567);
return 0;
}
8.5.1 the use of getchar and putchar
#include <stdio.h>
#include <ctype.h> // Needed for putchar()
#define SIZE 80
void reverse(const char *const sPtr); // Prototype
int main(void) {
char sentence[SIZE]; // Create char array
printf("Enter a line of text: ");
fgets(sentence, SIZE, stdin);
printf("\\nThe line printed backward is:\\n");
reverse(sentence); // Reverse the input sentence
putchar('\\n'); // Print a newline after the reversed sentence
return 0;
}
// Recursively outputs characters in string in reverse order
void reverse(const char *const sPtr) {
if (*sPtr == '\\0') { // Base case: if end of the string
return;
} else { // If not end of the string
reverse(sPtr + 1); // Recursion step
putchar(*sPtr); // Use putchar to display character
}
}
8.11
#include <stdio.h>
#define SIZE 80
int main(void) {
int c; // variable to hold character input by user
char sentence[SIZE]; // create char array
int i = 0; // initialize counter i
// prompt user to enter line of text
puts("Enter a line of text:");
// use getchar to read each character
while ((i < SIZE - 1) && (c = getchar()) != '\\n') {
sentence[i++] = c;
}
sentence[i] = '\\0'; // terminate string
// use puts to display sentence
puts("\\nThe line entered was:");
puts(sentence);
return 0;
}
8.12 sprintf function to determine which is int which is double
#include <stdio.h>
#define SIZE 80
int main(void) {
int x; // x value to be input
double y; // y value to be input
puts("Enter an integer and a double:");
scanf("%d%lf", &x, &y);
char s[SIZE]; // create char array
sprintf(s, "integer: %6d\\ndouble:%7.2f", x, y);
printf("%s\\n%s\\n", "The formatted output stored in array s is:", s);
return 0;
}
8.13 sscanf function
#include <stdio.h>
int main(void) {
char s[] = "31298 87.375"; // initialize array s
int x; // x value to be input
double y; // y value to be input
sscanf(s, "%d%lf", &x, &y);
printf("%s\\n%s%6d\\n%s%8.3f\\n",
"The values stored in character array s are:",
"integer:", x, "double:", y);
return 0;
}
string copy strcpy and strncpy: