C/C++实现:不使用字符串函数判断单词是否在句子中
判断目标单词是否存在于句子中的C/C++实现(不使用字符串库函数)
Got it, let's break this down. We need to check if a target word exists as a standalone word in a sentence—meaning it can't just be a substring of another word (like "cat" in "category" doesn't count). And we can't use any built-in string functions like strstr(), strlen(), or strcmp(). Here's a straightforward approach:
Core Logic Steps
- Manual Length Calculation: First, we'll write a helper function to get the length of any string by counting characters until we hit the null terminator (
'\0'). - Character-by-Character Matching: Traverse the sentence, and for each position, check if the following characters match the target word exactly.
- Boundary Check: Once we find a full match of the target word, we need to verify it's a standalone word—this means the character before the match is either a space or the start of the sentence, and the character after is either a space or the end of the sentence.
Complete Code Implementation
#include <iostream> using namespace std; // Helper function to get string length without using strlen() int getStringLength(char str[]) { int length = 0; while (str[length] != '\0') { length++; } return length; } // Function to check if target word exists as standalone in sentence bool isWordPresent(char sentence[], char target[]) { int sentenceLen = getStringLength(sentence); int targetLen = getStringLength(target); // If target is longer than sentence, it can't exist if (targetLen > sentenceLen) { return false; } // Traverse each possible starting position in the sentence for (int i = 0; i <= sentenceLen - targetLen; i++) { bool match = true; // Check if current segment matches target for (int j = 0; j < targetLen; j++) { if (sentence[i + j] != target[j]) { match = false; break; } } // If full match found, check word boundaries if (match) { // Check left boundary: start of sentence or space before bool leftValid = (i == 0) || (sentence[i - 1] == ' '); // Check right boundary: end of sentence or space after bool rightValid = (i + targetLen == sentenceLen) || (sentence[i + targetLen] == ' '); if (leftValid && rightValid) { return true; } } } return false; } int main() { char sentence[1000], target[100]; cout << "Enter the sentence: "; // Use fgets to read entire line (including spaces) fgets(sentence, sizeof(sentence), stdin); // Replace newline character from fgets with null terminator int senLen = getStringLength(sentence); if (senLen > 0 && sentence[senLen - 1] == '\n') { sentence[senLen - 1] = '\0'; } cout << "Enter the target word: "; cin >> target; if (isWordPresent(sentence, target)) { cout << "The word exists in the sentence." << endl; } else { cout << "The word does NOT exist in the sentence." << endl; } return 0; }
How It Works
- getStringLength: Simple loop that counts characters until it hits the null terminator—no library functions needed.
- isWordPresent:
- First, we rule out impossible cases where the target is longer than the sentence.
- For each starting index in the sentence, we check if the next
targetLencharacters match the target exactly. - When a full match is found, we check the boundaries to ensure it's a standalone word (not part of a longer word).
- main: Handles input (using
fgetsto capture the entire sentence including spaces, then cleaning up the newline character) and calls our check function to output the result.
Test Cases
- Case 1: Sentence = "I love coding in C++", Target = "coding" → Returns true (valid standalone word).
- Case 2: Sentence = "I love coding in C++", Target = "cod" → Returns false (only a substring of "coding").
- Case 3: Sentence = "Hello world", Target = "Hello" → Returns true (starts the sentence).
- Case 4: Sentence = "Hello world", Target = "world" → Returns true (ends the sentence).
内容的提问来源于stack exchange,提问作者Paras Jain
相关产品推荐
相关产品推荐

