Exercise 5-6. Rewrite appropriate programs from earlier chapters and exercises with pointers instead of array indexing. Good possibilities include getline() (Chapters 1 and 4), atoi(), itoa(), and their variants (Chapters 2, 3, and 4), reverse() (Chapter 3), and strindex() and getop() (Chapter 4).
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; // length
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; // initialize
char *p = s;
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
{*p++ = c;}
if (c == '\n') {*p++ = c;} // *p++ = '\n';
*p = '\0'; // properly end string
return (p-s); // line length
}
// return first index or t in s or -1 if not found
int strindex(char *s, char *t)
{
char *p, *q, *r;
for (p = s ; *p != '\0'; p++)
{
for (q = p, r = t; (*r != '\0') && (*q == *r); q++, r++)
{}
if ((r > t) && (*r == '\0'))
{return p-s;} // 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!
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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; // length
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; // initialize
char *p = s;
while(--lim > 0 && (c = getchar()) != EOF && c != '\n')
{*p++ = c;}
if (c == '\n') {*p++ = c;} // *p++ = '\n';
*p = '\0'; // properly end string
return (p-s); // line length
}
// return last index or t in s or -1 if not found
int strrindex(char *s, char *t)
{
char *p, *q, *r = t + strlen(t) - 1;
for (p = s+strlen(s)-1; p >= s; p--)
{
for (q = p; r >= t && *q == *r; q--, r--)
{}
if (r < t)
{return q-s+1;} // found, return last index
} // q+1-s
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!
*/