Ah Love! could you and I with Fate conspire
To grasp this sorry Scheme of Things entire,
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
#define MAXLINE 1000
int main(); // can be declared before definition
int getLine(char line[], int max); // getline() is declared by stdio.h
// searchfor[] in source[], return first index or -1 if not found:
int strindex(char source[], char searchfor[]);
int main()
{
char line[MAXLINE];
int found = 0, len;
char pattern[] = "ould";
// find all lines matching pattern:
while ((len = getLine(line, MAXLINE)) > 0)
{
if (strindex(line, pattern) >= 0)
{
printf("%s", line);
if (line[len-1] != '\n')
{putchar('\n');} // line may not end with '\n'
found++;
}
}
return found;
}
// get line into s[], return length
int getLine(char s[], int lim)
{
int c = EOF, i = 0; // initialize
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
{s[i++] = c;}
if (c == '\n') {s[i++] = c;} // s[i++] = '\n';
s[i] = '\0'; // properly end string
return i; // line length
}
// return first index or t[] in s[] or -1 if not found
int strindex(char s[], char t[])
{
int i, j, k;
for (i = 0; s[i] != '\0'; i++)
{
for (j = i, k = 0; t[k] != '\0' && s[j] == t[k]; j++, k++)
{}
if (k > 0 && t[k] == '\0')
{return i;} // found, return first index
}
return -1; // not found
}
/*
gcc pattern.c -o pattern
./pattern < Khayam.txt
// Omar Khayam, Rubaiyat (Edward Fitzgerald):
Ah Love! could you and I with Fate conspire
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 4-1. Write the function strrindex(s,t), which returns the position of the rightmost occurrence of t in the string s, or -1 if there is none.
Ah Love! could you and I with Fate conspire
To grasp this sorry Scheme of Things entire,
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
#include <string.h> // for strlen()
#define MAXLINE 1000
int main(); // can be declared before definition
int getLine(char line[], int max); // getline() is declared by stdio.h
// searchfor[] in source[], return last index or -1 if not found:
int strrindex(char source[], char searchfor[]);
int main()
{
char line[MAXLINE];
int found = 0, len;
char pattern[] = "ould";
// find all lines matching pattern:
while ((len = getLine(line, MAXLINE)) > 0)
{
if (strrindex(line, pattern) >= 0)
{
printf("%s", line);
if (line[len-1] != '\n')
{putchar('\n');} // line may not end with '\n'
found++;
}
}
return found;
}
// get line into s[], return length
int getLine(char s[], int lim)
{
int c = EOF, i = 0; // initialize
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
{s[i++] = c;}
if (c == '\n') {s[i++] = c;} // s[i++] = '\n';
s[i] = '\0'; // properly end string
return i; // line length
}
// return last index or t[] in s[] or -1 if not found
int strrindex(char s[], char t[])
{
int i, j, k;
int len_t = strlen(t);
for (i = strlen(s)-1; i >= 0; i--)
{
for (j = i, k = len_t-1; k >= 0 && s[j] == t[k]; j--, k--)
{}
if (k < 0)
{return i-len_t+1;} // found, return last index
} // i-(len_t-1)
return -1; // not found
}
/*
gcc patternr.c -o patternr
./patternr < Khayam.txt
// Omar Khayam, Rubaiyat (Edward Fitzgerald):
Ah Love! could you and I with Fate conspire
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#include <ctype.h> // for isspace(), isdigit()
#define SIZE 200 // max no of digits before and after decimal point
double atof(char []); // string to double
int main()
{
char fpn[SIZE]; // floating-point number
// valid fpn: 0, +0, -0, 0.0, -0.0, +0., 0., .0, -.0, +.0
printf("Type a floating-point decimal number " // concatenate strings
"(at least 1 digit) [+-]?[0-9]*[.]?[0-9]*\n"); // automatically
scanf("%s", fpn);
printf("%f\n", atof(fpn));
return 0;
}
double atof(char s[]) // string to double
{
double val, power;
int i, sign;
for (i = 0; isspace(s[i]); i++)
{} // skip whitespace
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
{i++;}
if ((s[i] == '\0') || (s[i] == '.' && s[i+1] == '\0'))
{
printf("\"%s\" is not a valid floating-point decimal number\n", s);
return 0.0; // should signal the error with an exception
}
for (val = 0.0; isdigit(s[i]); i++)
{ // ending '\0' of s[] is not a digit
val = 10.0 * val + (s[i] - '0');
}
if (s[i] == '.') // optional decimal point
{i++;} // skip decimal point, move to fractional part
for (power = 1.0; isdigit(s[i]); i++)
{
val = 10.0 * val + (s[i] - '0');
power *= 10.0; // power = power * 10.0;
}
return sign * val / power;
}
/*
gcc atof.c -o atof
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
.
"." is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
+
"+" is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-
"-" is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
+.
"+." is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-.
"-." is not a valid floating-point decimal number
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
+0.2
0.200000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-.5
-0.500000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
12.35
12.350000
/atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
0xa0 // we said decimal
0.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-2a1.23 // stops at non-decimal 'a'
-2.000000
./atof
Type a floating-point decimal number (at least 1 digit) [+-]?[0-9]*[.]?[0-9]*
-123.12a3 // stops at non-decimal 'a'
-123.120000
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 4-2. Extend atof() to handle scientific notation of the form 123.45e-6, where a floating-point number may be followed by e or E and an optionally signed exponent.
#include <stdio.h> // for printf(), scanf()
#include <ctype.h> // for isspace(), isdigit()
#define SIZE 200 // max no of digits before and after decimal point
double atof(char []); // string to double, optional scientific notation
int main()
{
char fpn[SIZE]; // floating-point number
double d;
// valid fpn: 0, +0, -0, 0.0, -0.0, +0., 0., .0, -.0, +.0
printf("Type a floating-point decimal number (at least 1 digit), followed by \n");
printf("an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?\n");
scanf("%s", fpn);
d = atof(fpn);
printf("%f\n", d);
printf("%g\n", d);
return 0;
}
double atof(char s[]) // string to double, optional scientific notation
{
double val, power;
int i, sign, exponent, expsign;
for (i = 0; isspace(s[i]); i++)
{} // skip whitespace
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
{i++;}
if ((s[i] == '\0') || (s[i] == '.' && s[i+1] == '\0'))
{
printf("\"%s\" is not a valid floating-point decimal number\n", s);
return 0.0; // should signal the error with an exception
}
for (val = 0.0; isdigit(s[i]); i++)
{ // ending '\0' of s[] is not a digit
val = 10.0 * val + (s[i] - '0');
}
if (s[i] == '.') // optional decimal point
{i++;} // skip decimal point, move to fractional part
for (power = 1.0; isdigit(s[i]); i++)
{
val = 10.0 * val + (s[i] - '0');
power *= 10.0; // power = power * 10.0;
}
if (s[i] == 'e' || s[i] == 'E') // optional exponent
{i++;} // skip 'e' or 'E', move to (signed) exponent
else {return sign * val / power;} // no exponent
// here there is an exponent
expsign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
{i++;} // skip sign, move to exponent
if (s[i] == '\0')
{
printf("\"%s\" is not a valid floating-point decimal number\n", s);
return 0.0; // should signal the error with an exception
}
for (exponent = 0; isdigit(s[i]); i++)
{ // ending '\0' of s[] is not a digit
exponent = 10 * exponent + (s[i] - '0');
}
while (exponent > 0) // while(exponent)
{
if (expsign < 0)
{power *= 10.0;} // we divide by power to find the return value
else {power /= 10.0;}
exponent--;
}
return sign * val / power;
}
/*
gcc atog.c -o atog
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
.
"." is not a valid floating-point decimal number
0.000000
0
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
+
"+" is not a valid floating-point decimal number
0.000000
0
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
-
"-" is not a valid floating-point decimal number
0.000000
0
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
+.
"+." is not a valid floating-point decimal number
0.000000
0
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
-.
"-." is not a valid floating-point decimal number
0.000000
0
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
+0.2
0.200000
0.2
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
-.5
-0.500000
-0.5
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
-2a1.23 // stops at non-decimal 'a'
-2.000000
-2
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
0.2e
"0.2e" is not a valid floating-point decimal number
0.000000
0
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
+2.3e+
"+2.3e+" is not a valid floating-point decimal number
0.000000
0
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
123.45e-6
0.000123
0.00012345
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
123.45E+6
123450000.000000
1.2345e+08
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
123.45e6
123450000.000000
1.2345e+08
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
123.45e12
123449999999999.968750
1.2345e+14
./atog
Type a floating-point decimal number (at least 1 digit), followed by
an optional scientific notation, [+-]?[0-9]*[.]?[0-9]*([eE][+-]?[0-9]+)?
123.45e-25
0.000000
1.2345e-23
*/