Search a string in a text file in C


 
Thread Tools Search this Thread
Top Forums Programming Search a string in a text file in C
# 1  
Old 05-11-2014
Search a string in a text file in C

Hello guys,
i want some help please in coding that program , " A mini dictionary"

the file looks like :

Code:
 Waver     ---       To be hesitated
 retirement ---       life after end of career

The user enter the word , and then it prints the meaning of it
Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, output, and code fragments.

Last edited by Don Cragun; 05-11-2014 at 01:28 AM.. Reason: Add CODE tags.
# 2  
Old 05-11-2014
Is this a homework assignment?

If not, is the intent that your program will read the dictionary into memory and repeatedly search for words entered by the user, or that your program will search for a single word in your dictionary and exit? Does the user have supply the leading and trailing spaces before the separating --- in your dictionary. Does the user have to match case as it appears in your dictionary, or is your program supposed to do a case insensitive search?

What have you tried so far?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-11-2014
No , it's not. It's a project in my college

i used that "---" only to separate words and meanings , it will appear in the program as a Space
The mini-dictionary already has words and their corresponding meanings. The
user input a sentence (string of characters) to perform one of the following
operations on it.

The Operations are:
- Separate sentence into tokens (i.e. separate words)
- Get the frequency of each word (i.e. number of repetitions of each word),
from the sentence.
- Display the corresponding meaning, from the dictionary, for each unrepeated
token (a word) in given sentence.

And ask the user if he wants to perform any additional operation on this sentence
- If yes, let him choose another operation.
- If no, ask the user if he wants to enter a new sentence.
-If yes, display the menu from beginning and ask him to enter a new
sentence
- if no, exit from the program

Last edited by Alyy; 05-11-2014 at 11:38 AM..
# 4  
Old 05-11-2014
If this isn't homework, why use C? A higher level language is a much better fit.

Regards,
Alister
# 5  
Old 05-11-2014
That what i had tried till now.


Code:
#include <stdio.h>
#include <stdlib.h>
int main()
 {
     char sentence[30];
    int operation;
    char *ptr;
    printf ("\n\n                              USER MENU...\n\n\n");
    printf ("1 - Press Number 1 to Separate sentence into tokens.\n\n");
    printf ("2 - Press Number 2 to get the frequency of each word.\n\n");
    printf ("3 - Press Number 3 to display the corresponding meaning, from the dictionary.\n\n\n");
    printf("Enter The Sentence First ....\n\n ");
    gets(sentence);
    printf ("\n\n");
    printf ("Choose the operation you want from USER MENU...\n\n");
    scanf ("%d",&operation);
    printf ("\n");
    switch (operation)
    {
    case 1:
         ptr = strtok (sentence," .");
  while (ptr != NULL)
  {
    printf ("%s\n",ptr);
    ptr = strtok (NULL, " .");
  }
  break;
    }



    return 0;
}

Sorry for long comment

---------- Post updated at 09:22 AM ---------- Previous update was at 09:19 AM ----------

Quote:
Originally Posted by alister
If this isn't homework, why use C? A higher level language is a much better fit.

Regards,
Alister
I have asked many which language to learn first , they told me to learn C firstly Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line1 Line2 ...... (4 Replies)
Discussion started by: Akilleez
4 Replies

6. Shell Programming and Scripting

search for a string in a text file

I want to write a script to check for duplicates For example: I have a text file with information in the format of /etc/passwd alice:x:1008:555:William Williams:/home/bill:/bin/bash bob:x:1018:588:Bobs Boos:/home/bob:/bin/bash bob:x:1019:528:Robt Ross:/home/bob:/bin/bash james:x:1012:518:Tilly... (3 Replies)
Discussion started by: ixDLxi
3 Replies

7. UNIX for Dummies Questions & Answers

Unix search a string in the text file

File name : Sample.txt Actually i would like to read <schema>Oracle<schema> string from input file and return only once database as my output. Please advise me. Moved to appropriate forum. (1 Reply)
Discussion started by: balajikalai
1 Replies

8. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question