i have two questions in this post:
1.
working on a program from a book.
the program looks for a string, which happens to be "ould"
the book gives as an example the lines:
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!
the book says that the above lines should produce the following output:
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!
the problem i have been having when i run the program, which compiled in terminal without any errors, is that when i enter a line and then press the enter key
the computer immediatly repeats the line. i think the computer is not recognizing the newline character '\n'.
here is my source file which i called string.c
is there a way to have the computer wait for EOF before printing the lines that contain the string "ould"?
question #2.
can someone help me turn a function into a program?
i have a function from a book i am reading. it is supposed to reverse the string s in place. here is the function:
how would i go about turning this function into a executable file?
what i want the program to do is take a character string,
like i would type:
abcd
and the computer would respond with
dcba
any help would be appreciated.
1.
working on a program from a book.
the program looks for a string, which happens to be "ould"
the book gives as an example the lines:
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!
the book says that the above lines should produce the following output:
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!
the problem i have been having when i run the program, which compiled in terminal without any errors, is that when i enter a line and then press the enter key
the computer immediatly repeats the line. i think the computer is not recognizing the newline character '\n'.
here is my source file which i called string.c
Code:
#include <stdio.h>
#define MAXLINE 1000 /* maiximum input line length */
int getline(char line[], int max);
int strindex(char source[], char searchfor[]);
char pattern[] = "ould"; /* pattern to search for */
/* find all lines matching pattern */
main()
{
char line[MAXLINE];
int found = 0;
while (getline(line, MAXLINE) > 0)
if (strindex(line, pattern) >= 0) {
printf("%s", line);
found++;
}
return found;
}
/* getline: get line into s, return length */
int getline(char s[], int lim)
{
int c, i;
i = 0;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
/* strindex: return index of t in s, -1 if none */
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;
}
return -1;
}
is there a way to have the computer wait for EOF before printing the lines that contain the string "ould"?
question #2.
can someone help me turn a function into a program?
i have a function from a book i am reading. it is supposed to reverse the string s in place. here is the function:
Code:
#include <string.h>
/* reverse: reverse string s in place */
void reverse(char s[])
{
int c, i, j;
for (i = 0, j = strlen(s)-1; i < j; i++, j--) {
c = s[i];
s[i] = s[j]
s[j] = c;
}
}
how would i go about turning this function into a executable file?
what i want the program to do is take a character string,
like i would type:
abcd
and the computer would respond with
dcba
any help would be appreciated.