V
V
vitalianno2016-06-19 16:04:59
C++ / C#
vitalianno, 2016-06-19 16:04:59

How to implement correct string search in C?

The crux of the matter is this. The result of the work of the presented program is to search for lines that contain the keyword and display them on the screen after input. That is, it should look something like the following:
Modeled on "ould" from

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!
Ah Love!
lines must be selected .
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!

But the problem is that the output occurs immediately after the first such line is found, and not after the end of the entire text input.
Please help me figure out how to organize the output of the detected lines exactly after the end of the input - let's say after a double \n.
Thank you.

#include <stdio.h>
 
#define MAXLINE 1000 /* максимальный размер вводимой строки */
 
int getline(char line[], int max);
 
int strindex(char source[], char searchfor[]);
 
 
char pattern[] = "ould"; /* образец для поиска (литерал) */ 
 
/* найти все строки, содержащие образец */
 
 int main(void)
 
{
    char line[MAXLINE];
    int found = 0;
 
 
    while (getline(line, MAXLINE) > 0)
 
        if (strindex(line, pattern) >= 0){
            printf ("%s\n", line);
            found++;
        }
 
    return found; 
}
 
/* getline: читает строку в s, возвращает длину */
 
int getline(char s[], int lim)
{
    int c, i;
    
    
    for (i = 0; i < lim && (c = getchar()) != EOF && c != '\n'; i++){
 
        s[i] = c;
    }
 
    if (c == '\n') s[i] = c;
 
    s[i] = '\0';
            
    return i;
}
 
/* strindex: вычисляет место t в s или выдает -1, если t нет в s */
 
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;
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
#
#algooptimize #bottize, 2016-06-19
@user004

remove the print and save the found lines

N
NeoCode, 2016-06-19
@NeoCode

If this is pure C and there is no way to use stl containers, then declare a two-dimensional array (for several lines) instead of a one-dimensional array (for one line).
Then first make a loop of reading lines into it - no output
And then the next loop - by the number of lines read - processing and output

A
abcd0x00, 2016-06-21
@abcd0x00

Please help me figure out how to organize the output of the detected lines exactly after the end of the input - let's say after a double \n.

It is necessary to compile the program, and then submit the entire text through the channel to its input.
Line:
Windows:
type file.txt | prog.exe

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question