write a program to Finding words that begin with specific letter "s" in text file.

Hi Experts,

Question about File handling in C++ ....

write a program to Finding all words that begin with specific letter "s" in text file?
Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.


File I/O is no different from other I/O; the standard library has the stream abstraction:
http://www.cplusplus.com/doc/tutorial/files/

WHILE read word from stream
  IF word starts with s
  THEN do something
plz help, this program is very important for me.
plz help, this program is very important for me.

It's a pretty easy program. If you can't write it then maybe it's best that you fail the course. If you've made an attempt then post your code and we can help you fix it.
plz help, this program is very important for me.

We would love to help you fix your code. But we can't, if you don't show us your code.
#include <iostream>
#include <fstream>
using namespace std;

int main () {
ifstream input;
string filename="file.txt";
string word;

input.open(filename.c_str());
int len=word.length();

while(getline(input,word)) {

if(word.at(len-1)='a') {
cout<<word;
}

}
}
Last edited on
try if (word.length() > 0 && word.at(0) == 'a') instead.

Notice that it's == (equality), not = (assignment).
Last edited on
showing blank output...
Notice that you declare "word" in line...um let me count...8 (always post your code within code tags--click the "<>" format button to create the tags and paste your code in between them).

Then in line 11, while word is still empty, you save the "len", which, because word is empty, has a value of 0.

In line 13 you start looping through the file and reading each line and assigning it to "word". You never recalculate the value "len", so it is always 0. Because of that the

if (word.length() > 0 && word.at(0) == 'a') statement will always fail, and you will never print out a word.
kuldeep wrote:
showing blank output...

Gee, I sure wish I were psychic, to know what you actually changed!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
using namespace std;

int main () {
    ifstream input;
    string filename="file.txt";
    string word;

    input.open(filename.c_str());
    int len=word.length(); // useless

    while(getline(input,word)) {

        //if(word.at(len-1)='a')
        if (word.length() > 0 && word.at(0) == 'a') 
        {
            cout<<word;
        }

    }
}


input:
aardvark
apple
banana
anana
nana
nani???


output:
aardvarkappleanana


Obviously the above can be cleaned up more. I just changed the one line to prove that what I said does work.

PS: If you're delimiting based on words and not lines, use operator>>(std::istream&, std::string) and not std::getline.
Last edited on
but i am giving input in given below format then after it is not working:

aardvark apple banana anana nana nani???
Those aren't lines! Your own post had you using getline. Use input >> variable; to extract tokens delimited by whitespace.
Last edited on
Why not just read it char by char? You'd surely find an S that way
Thank you so much "Ganado" sir.....
Topic archived. No new replies allowed.