2

I have a text file called aisha

This is a new file I did it for mediu. Its about Removing stopwords fRom the file and apply casefolding to it I Tried doing that many Times and finally now I could do 

and I did a code to read that text file and save it into an array and then convert some characters to lowrcase but what I want is to to make the codes reads the file as a string not char

char myArray[200]; 

to be

`string myArray[200];` 

I think I can do it using the function tolower()and a string std insted that long code I used but I dont know how to change my code to a code that uses that functions

my code is

#include <iostream> #include <string> #include <fstream> #include<ctype.h> int main() { using namespace std; ifstream file("aisha.txt"); if(file.is_open()) { file >> std::noskipws; char myArray[200]; for(int i = 0; i < 200; ++i) { cout<<"i"; if (myArray[i]=='A') cout<<"a"; if (myArray[i]=='T') cout<<"t"; if (myArray[i]=='R') cout<<"r"; else if (myArray[i]!='I' && myArray[i]!='T' && myArray[i]!='R'&& myArray[i]!='A') cout<<myArray[i]; } file.close(); } system("PAUSE"); return 0; } 

I saw that solution in this site but I couldnt apply it to my code

#include <boost/algorithm/string.hpp> std::string str = "wHatEver"; boost::to_lower(str); Otherwise, you may use std::transform: std::string str = "wHatEver"; std::transform(str.begin(), str.end(), str.begin(), ::tolower); 

4 Answers 4

3

The following code can solve your problem:

#include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <string> #include <cctype> #include <algorithm> #include <iterator> using namespace std; int main(int argc, char **argv) { ifstream ifs("file"); ostringstream oss; char c; while(true) { c = ifs.get(); if(ifs.good()) oss.put(c); else break; } string s = oss.str(); transform(s.begin(), s.end(), ostream_iterator<char>(cout), ::tolower); return 0; } 
Sign up to request clarification or add additional context in comments.

5 Comments

What do you see in your terminal after execution of compiled code?
there is an error here #include <boost/algorithm/string.hpp> error : no such file directory I use dev c++
do you have boost installed?
error in this lie #include <boost/algorithm/string.hpp> the dicription : boost/algorithm/string.hpp no such file directory
amm sorry how can install it || I am a biginner
2

1)i hope this example helps you

#include "stdafx.h" #include "iostream" #include <stdio.h> #include <ctype.h> using namespace std; int main () { int i=0; char str[30]="TESt STRING.\n"; char c; while (str[i]) { c=str[i]; str[i]= (tolower(c)); cout<<str[i]; i++; } return 0; } 

3 Comments

I will try it but I dont have this directory stdafx.h how can I install it ?
@AishaAhmedAhmed: Ignore that stdafx.h, it's a Microsoft-specific thing for precompiled headers and completely optional.
if u are using blank project then ignore it other wise it will work in 2008 and higher
1

See the solutions here: std::tolower and Visual Studio 2013

For example, you could do:

std::transform(test.begin(), test.end(), test.begin(), [](unsigned char c) { return std::tolower(c); }); 

Comments

1

The are any number of ways to do this. In the end you want a container that container all lower-case strings of the input file data, it is fairly straight-forward. The premise of the algorithm used here is:

  • use a std::istream_iterator<std::string> pair to load a vector of strings from the input file.
  • use the std::for_each algorithm to enumerate over each string in the vector, performing a custom action.
  • the custom action we're performing is to enumerate over each character in the current string, transforming the character to its lower-case equivalence.

In the end, the code looks something like this:

#include <iostream> #include <fstream> #include <iterator> #include <algorithm> #include <vector> #include <string> #include <cctype> // transform a string in-place to lower case struct StrLower { void operator()(std::string& s) const { for (std::string::iterator it = s.begin(); it != s.end(); ++it) *it = static_cast<char>(std::tolower(static_cast<unsigned char>(*it))); } }; int main(int argc, char *argv[]) { // build a vector of std::string from the input file std::ifstream inf("aisha.txt"); std::vector<std::string> data ( (std::istream_iterator<std::string>(inf)), std::istream_iterator<std::string>()); // for each string in the vector, transform to lower case. for_each(data.begin(), data.end(), StrLower()); // display all the transformed strings std::copy(data.begin(), data.end(), std::ostream_iterator<std::string>(std::cout, "\n")); return 0; } 

Input (aisha.txt)

This is a new file I did it for mediu. Its about Removing stopwords fRom the file and apply casefolding to it I Tried doing that many Times and finally now I could do 

Output

this is a new file i did it for mediu. its about removing stopwords from the file and apply casefolding to it i tried doing that many times and finally now i could do 

Hope that helps you out.

5 Comments

ok there are some errors but maybe because of my version of devcc++ I will chck it || thanks alot for helping
@AishaAhmedAhmed Ok. I pulled all the C++11 specific code out, what is left should work with any C++ compiler and library set made in the last decade (at least). Good luck.
@AishaAhmedAhmed no problem. Each of those things you see prefaced with std:: that you're unfamiliar with in the above code should be studied on a reputable source, such as www.cppreference.com. You already likely know about std::string. the rest you should learn more about. Take the time to do so; it will pay off big-time later.
yes I am still a biginner and after finishing this semester I would try my best to understand and learn these codes because it will make programming easier
@AishaAhmedAhmed you will be amazed how often algorithms from the standard library can be used to do things you would normally think you need to hand-code. It is an incredible library. Take the time to learn it; it is well worth it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.