![](/img/trans.png)
[英]strstr to find a word in a string using c returning true all the time
[英]Seperating all characters in a string before a strstr pointer in C
我正在用 C 構建一個查找和替換程序。它需要能夠用其他東西替換在文本文件中搜索的任何內容。
例如。 在 input.txt 中搜索 elo 替換為 ELO
結果:開發人員 devELOpers
例如搜索 el 替換 EL 你好你好歡迎歡迎
我在獲取指針之前的字符串中的所有字符時遇到了真正的麻煩。 我可以獲得單個詞和搜索詞的指針,我可以獲得搜索詞之后的所有內容,但我無法獲得搜索字符之前的所有內容。 在整個單詞上嘗試 strlen 並減去無濟於事。 我也可以得到我需要的單詞的第一部分的長度。 謝謝你的幫助。
在經營中尋找時代:
輸出是
firstMatch= 評分
結束部分=ting
起始長度 2
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#define LINE_LENGTH 1000
//Function to remove a newline character and replace it with a null terminator.
void remove_newline(char *str)
{
int len =strlen (str);
if (len>0 &&str[len -1] == '\n')
str[len -1] = '0';
}
int main(int argc, char **argv)
{
char* searchWord = argv[1]; //Define the string searched for as argument 1.
char* replaceWord = ""; // Initialise the word for replacing search word.
char* text_File= ""; // Initialise the text file
for(int i = 0; i < argc; i++)
{
if (strcmp(argv[i],"-r")==0)
{
replaceWord = argv[i+1]; // Identify the replacemnt word as the argument that comes after "-r".
}
if (strcmp(argv[i],"-i")==0)
{
text_File = argv[i+1]; // Identify the file word as the argument that comes after "-i".
}
}
char slen= strlen(searchWord);
printf("You have searched for %s\n", searchWord); // Clarify for the user their search terms and input file.
printf("In the file %s\n", text_File);
FILE *input_file = fopen(text_File, "r"); // Open the text file
char line [LINE_LENGTH];
FILE *write_file = fopen("output.txt", "w"); //Create a new file for writing to.
` while (fgets(line, LINE_LENGTH, input_file) != NULL) ***// Loop through the contents of the input file.***
{
char *currentWord; ***// Separate the words at any of "\n ,.-".***
char *tempWord;
currentWord = strtok(line, "\n ,.-");
while (currentWord != NULL) ***// Loop through every seperated word.***
{
remove_newline(currentWord); //Remove the newline character form the current word.
if (strstr(currentWord, searchWord)!= NULL) ***// Check if the current word contains the searh word***
{
printf ("%s ", currentWord); ***// If it does print to console the word containing it***
char currLength= strlen(currentWord);
printf("%d\n", currLength);
char* firstMatch= strstr(currentWord, searchWord); ***// Find the first occurence of the searched term***
printf ("firstMatch %s\n ", firstMatch); ***//Print evrything after and including search.***
char* lastPart = firstMatch + slen; ***// Get the part after the search***
printf ("End part %s\n ", lastPart);
char rest = strlen(firstMatch);
char startLength = currLength - rest;
這是它不起作用的地方。
char* startPart = firstMatch - startLength;
printf ("start Part %s\n ", startPart);
printf ("startLength %d\n\n ", startLength);`
當您嘗試打印單詞的“匹配之前”部分時出現意外結果的原因是單詞字符串中沒有任何內容會導致
printf("start Part %s\n ", startPart);
在打印單詞的第一個startLength
字符后調用 stop。 當printf
被告知要打印一個字符串時,它會打印從起點到遇到\\0
終止符的所有字符。 在這里,唯一的\\0
位於單詞的末尾,因此printf
打印整個單詞。
如果您只想打印單詞的前幾個字符,則必須構造一個僅包含前幾個字符的\\0
終止字符串,或者您必須使用不嘗試將它們視為一個字符串。
為了構建一個\\0
封端的啟動串,你可以暫時地以覆蓋本場比賽的第一個字符\\0
,然后調用printf
,然后恢復比賽字符。 就像是:
char savedFirstMatch = *firstMatch;
*firstMatch = '\0';
printf("start Part %s\n ", startPart);
*firstMatch = savedFirstMatch;
如果您不想這樣做,那么您可以使用for
循環僅將第一個startLength
字符打印為單個字符,而不是作為字符串,前后是printf
或puts
,它會發出您想要打印的任何額外內容那些字符。 在這種情況下,額外的東西是字符之前的“開始部分”字符串,以及之后的換行符和空格(假設該空格不僅僅是一個錯字)。 那看起來像:
puts("start Part ");
unsigned startIndex;
for (startIndex = 0; startIndex < startLength; ++startIndex) {
putchar(startPart[startIndex]);
}
puts("\n ");
當然,如果您對puts
和putchar
不滿意,您可以使用printf("%s", ...)
和printf("%c", ...)
代替。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.