I am reading from a file which has words like:

connecting

classes

feeds

I need to convert each character to lowercase and then call a function to remove suffix from each word. Say, first on connecting, then on classes...

I am done with rest of the part but have a problem reading the file and storing words in array.

I will have a minimum of 50 such words in the file. What is the best way to store it?

This is what I have done:

{ int val=0; char fin_char; 
string line;string arr[100]; 
ifstream myfile("testfile.txt"); 
if (myfile.is_open()) 

while(myfile.good()) 
{ 
getline(myfile,line); 
arr[i]=line; 
i++; 
} 
myfile.close(); 
for (int j=0;j { 
while (arr[j][k]!='\0') 
{ 
c=arr[j][k]; 
cout<<"C"< val=int(c); 
if (val>=65&&val<=90){ val=val+32;fin_char=static_cast(val);arr[j][k]=fin_char;} 
k++; 
} 
} 
for (int j=0;j { 
cout<<" "< }

The output I get is of the form:
C 99 J:0 K:0
C 111 J:0 K:1
C 110 J:0 K:2
C 110 J:0 K:3 `

remove suffix from each word. Say, first on connecting, then on classes.

You mean change "connecting" to "connect" and "classes" to "class"?

The code you posed makes no sense, some lines are incomplete and other just unreadable. Example: line 13 is incomplete, no compiler would be able to successfully compile that line. line 18 is nearly unreadable -- split it up into individual lines to make the code more readable. Putting everything on one line like that is very bad programming style. Also you need to learn to properly indent the code to make it readable. I hope your teacher didn't teach that programming style. If he did then you need to get a different teacher.

Edited 3 Years Ago by Ancient Dragon

If you get that output it must be from a different program.

When I do a compile even with the standard header files I get:

x@x-Calistoga-ICH7M-Chipset:~/Documents/cpp$ g++ -Wall blah.cpp
blah.cpp: In function ‘int main()’:
blah.cpp:16:17: error: ‘i’ was not declared in this scope
blah.cpp:20:23: error: expected ‘;’ before ‘{’ token
blah.cpp:20:23: error: expected primary-expression before ‘{’ token
blah.cpp:20:23: error: expected ‘)’ before ‘{’ token
blah.cpp:21:19: error: ‘k’ was not declared in this scope
blah.cpp:23:13: error: ‘c’ was not declared in this scope
blah.cpp:24:27: error: no match for ‘operator<’ in ‘std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char>&)(& std::cout)), ((const char*)"C")) < val’
blah.cpp:28:39: error: expected ‘<’ before ‘(’ token
blah.cpp:28:39: error: expected type-specifier before ‘(’ token
blah.cpp:28:39: error: expected ‘>’ before ‘(’ token
blah.cpp:34:23: error: expected ‘;’ before ‘{’ token
blah.cpp:34:23: error: expected primary-expression before ‘{’ token
blah.cpp:34:23: error: expected ‘)’ before ‘{’ token
blah.cpp:36:1: error: expected primary-expression before ‘}’ token
blah.cpp:36:1: error: expected ‘;’ before ‘}’ token
blah.cpp:36:1: error: expected ‘}’ at end of input

Yes "connecting" to "connect", "classes" to "class"
I would paste the exact code here.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{


char temp[15];
int c=0; int k=0;
int i=0;
int val=0; char fin_char;
string line;string arr[100];
ifstream myfile("testfile.txt");
if (myfile.is_open())
{
    while(myfile.good())
    {
    getline(myfile,line);
    arr[i]=line;
    i++;
    }
    myfile.close();
    for (int j=0;j<i;j++)
    {
    while (arr[j][k]!='\0')
    {
    c=arr[j][k];
    cout<<"C"<<c<<" "<<"J:"<<" "<<j<<"K:"<<k<<"\n";
    val=int(c);
    if (val>=65&&val<=90)
    { 
                         val=val+32;
                         fin_char=static_cast<char>(val);
                         arr[j][k]=fin_char;
    }
    k++;
    }
}
for (int j=0;j<i-1;j++)
{
    cout<<" "<<arr[j]<<"\n";
}
system("pause");
return 0;
}
}

I know how to do the removing the suffix part but I need the word in form of a char array.
say "classes" as arr[0]="c", arr[1]="l", arr[2]="a",....
The way I am reading it, draws a line from the file.
Is there any way I could possibly add it in a char array and then perform the necessary operations?

If you want the C-style string (null terminated sequences of characters), you can get it by the c_str() function from the std::string class. Click Here
As for getting the sufix, try using the substr(startpos, stoppos) function. Click Here

Also, here's a small example of how you can convert a string to lowercase:

void getLower(string &str){
    for (size_t i=0;i<str.size();i++)
        str[i] = tolower(str[i]);
}

And here's a small example of how you can remove suffixes of the words using substr()

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <set>
using namespace std;

void getLower(string &str){
    for (size_t i=0;i<str.size();i++)
        str[i] = tolower(str[i]);
}

int main(){
    string suf[3] = { "es" , "ing" , "ds" },                                           //suffixes
           items[3] = { "ConNecting", "claSsEs", "feEdS" };                            //words
    set<string> suffixes(suf, suf + sizeof(suf) / sizeof(string));
    vector<string> elements(items, items + sizeof(items) / sizeof(string));
    for (int i=0;i<(int)elements.size();i++){
        getLower(elements[i]);                                                         //gets the lowercase of that string
        int elemsize = elements[i].size();
        if (suffixes.find(elements[i].substr(elemsize-2, elemsize)) != suffixes.end()) //checks for suffixes
            elements[i].erase(elemsize-2, elemsize);                                   //if found, remove them
        else if (suffixes.find(elements[i].substr(elemsize-3, elemsize)) != suffixes.end())
            elements[i].erase(elemsize-3, elemsize);
    }
    for (int i=0;i<(int)elements.size();i++)
        cout<<elements[i]<<endl;
    return 0;
}

Edited 3 Years Ago by Lucaci Andrew

This article has been dead for over six months. Start a new discussion instead.