Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <stdlib.h>
- bool isFilePath(char *string){
- return access(string, F_OK) == 0 ? true : false;
- }
- int main(int argc, char **argv){
- char filepath[20];
- bool isFile = false;
- bool hasC = false;
- bool hasI = false;
- int searchedWordIndex;
- for (int i = 1; i < argc; i++) {
- // printf("arg: %s\n",argv[i]);
- // printf("i: %d\n",i);
- if(strcmp("-c",argv[i]) == 0){
- // puts("entrou no primeiro");
- hasC = true;
- continue;
- }
- if(strcmp("-i",argv[i]) == 0){
- // puts("entrou no segundo");
- hasI = true;
- continue;
- }
- if(isFilePath(argv[i])){
- // puts("entrou no terceiro");
- isFile = true;
- strcpy(filepath, argv[i]);
- continue;
- }
- // puts("chegou no quarto");
- searchedWordIndex = i;
- }
- int searchedWordLength = strlen(argv[searchedWordIndex]);
- char searchedWord[searchedWordLength];
- strcpy(searchedWord,argv[searchedWordIndex]);
- // printf("Palava procurada é %s na posição %d e tem tamanho %d\n",searchedWord,searchedWordIndex,searchedWordLength);
- int count = 0;
- FILE *input = stdin;
- if (isFile)
- {
- input = fopen(filepath, "r");
- if(input==NULL){
- printf("[ERRO AO ABRIR O ARQUIVO]\n");
- exit(1);
- }
- }
- char line[256];
- char loweredLine[256];
- fgets(line, sizeof(line), input);
- while (fgets(line, sizeof(line), input)) {
- strcpy(loweredLine,line);
- if (hasI)
- for (int i = 0; i < strlen(line); i++)
- loweredLine[i]=tolower(loweredLine[i]);
- // printf("Procurando em: %s",loweredLine);
- if(strstr(loweredLine, searchedWord)){
- count++;
- if(!hasC)
- printf("%s",line);
- }
- }
- if(hasC)
- printf("%d\n",count);
- if(isFile)
- fclose(input);
- return 0;
- }
- char *lowerString(char* text, int text_size){
- char *lowered;
- for(int i = 0; i < text_size; i++){
- lowered[i] = tolower(text[i]);
- }
- return lowered;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement