C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
how to letters in every word and change to numbers Posted by CplusplusNubie on 20 Apr 2007 at 7:28 AM
How do you count the letters in every word of an external file, then replace the words by the numbers in each word? I am able to count the letters, but I do not understand how to change and replace the letters to numbers. Any suggestions?
Report
Re: how to letters in every word and change to numbers Posted by bilderbikkel on 20 Apr 2007 at 7:37 AM
: How do you count the letters in every word of an external file, then replace the words by the numbers in each word? I am able to count the letters, but I do not understand how to change and replace the letters to numbers. Any suggestions?
:

Use the index operator (the square brackets):

std::string myWord = "Hello";
myWord[1] = 'a';


Also works on C-style strings!

See ya,



bilderbikkel

Report
Re: how to letters in every word and change to numbers Posted by IDK on 20 Apr 2007 at 10:36 AM
: : How do you count the letters in every word of an external file, then replace the words by the numbers in each word? I am able to count the letters, but I do not understand how to change and replace the letters to numbers. Any suggestions?
: :
:
: Use the index operator (the square brackets):
:
:
: std::string myWord = "Hello";
: myWord[1] = 'a';
: 

:
: Also works on C-style strings!
:
: See ya,
:
:
:
: bilderbikkel
:
:

So you would probbably read the whole thing into buffer before procesing...

Another way would be to do the classic write to another file, remove the first and rename the second to the name of the first.
Report
Re: how to letters in every word and change to numbers Posted by CplusplusNubie on 20 Apr 2007 at 11:33 AM
: : : How do you count the letters in every word of an external file, then replace the words by the numbers in each word? I am able to count the letters, but I do not understand how to change and replace the letters to numbers. Any suggestions?
: : :
: :
: : Use the index operator (the square brackets):
: :
: :
: : std::string myWord = "Hello";
: : myWord[1] = 'a';
: : 

: :
: : Also works on C-style strings!
: :
: : See ya,
: :
: :
: :
: : bilderbikkel
: :
: :
:
: So you would probbably read the whole thing into buffer before procesing...
:
: Another way would be to do the classic write to another file, remove the first and rename the second to the name of the first.
:

huh?!

Report
Re: how to letters in every word and change to numbers Posted by IDK on 20 Apr 2007 at 12:44 PM

: : So you would probbably read the whole thing into buffer before procesing...
: :
: : Another way would be to do the classic write to another file, remove the first and rename the second to the name of the first.
: :
:
: huh?!
:
:

Show me your code and I'll explain
Report
Re: how to letters in every word and change to numbers Posted by CplusplusNubie on 20 Apr 2007 at 1:37 PM
:
Here is my code. I am trying to get teh program to read an external file, counts the letters in every word, then replaces the word with the number, then writes to an external file. I know that I am messed up, I don't understand where, or how to get the middle part of the question.

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>


using namespace std;

void LetterCount (void);


int letter;
char letter, inputChar;

string inputWord;
ifstream inFile;
ofstream outFile;

int main ()
{

inFile.open ("input.txt");

outFile.open ("output.txt");

letter=0;


while (inFile>>inputLetter)

LetterCount ();

while (inFile>>letter)


cout<<"Letter Count is "<<letter<<"."<<endl;

return 0;
}

void LetterCount (void)
{
letter++;
}

Report
Re: how to letters in every word and change to numbers Posted by IDK on 20 Apr 2007 at 2:22 PM
: :
: Here is my code. I am trying to get teh program to read an external file, counts the letters in every word, then replaces the word with the number, then writes to an external file. I know that I am messed up, I don't understand where, or how to get the middle part of the question.
:
: #include <iostream>
: #include <fstream>
: #include <string>
: #include <cstdlib>
: 
: 
: using namespace std;
: 
: void LetterCount (void);
: 
: 
: int letter;
: char letter, inputChar;
: 
: string inputWord;
: ifstream inFile;
: ofstream outFile;
: 
: int main ()
: {
: 
:   inFile.open ("input.txt");
: 
:   outFile.open ("output.txt");
: 
:   letter=0;
: 
: 
:   while (inFile>>inputLetter)
:     LetterCount ();
: 
:   while (inFile>>letter)
:     cout<<"Letter Count is "<<letter<<"."<<endl;
: 
:   return 0;
: }
: 
: void LetterCount (void)
: {
: letter++;
: }


Please use code tags when posting code (as I've done above, besides from formating your code a little)

Are the words separated by zeroes?

And this code doesn't do anything, but count the number of chars in a file.

This is how I would do it (uncompiled code):
#include <fstream>

int main(){
  char letterC, c;

  inFile.open ("input.txt");
  outFile.open ("output.txt");

  while(!inFile.eof()){
    do{
      inFile>>c;
      letterC++;
    }while(c!=' ');

    outFile << letterC;
  }

  outFile.flush();
  inFile.close();
  outFile.close();
}

Report
Re: how to letters in every word and change to numbers Posted by velius on 20 Apr 2007 at 3:16 PM
: #include <iostream>
: #include <fstream>
: #include <string>
: #include <cstdlib>
: 
: 
: using namespace std;
: 
: void LetterCount (void);
: 
: 
: int letter;
: char letter, inputChar;
: 
: string inputWord;
: ifstream inFile;
: ofstream outFile;
: 
: int main ()
: {
: 
: inFile.open ("input.txt");
: 
: outFile.open ("output.txt");
: 
: letter=0;
: 
: 
: while (inFile>>inputLetter)
: 
: LetterCount ();
: 
: while (inFile>>letter)
: 
: 
: cout<<"Letter Count is "<<letter<<"."<<endl;
: 
: return 0;
: }
: 
: void LetterCount (void)
: {
: letter++;
: }


If I understand you correctly you're looking to do something like this?

Say the file contains 3 words such as: One two three

And you want to count them and output that to a file so the output would be to the file: 3 3 5

#include <fstream>

int main(int argc, char** argv)
{
    int count = 0;
    char ch;
    ifstream infile;
    ofstream outfile;

    infile.open("input.txt");
    outfile.open("output.txt");

    while(!infile.eof())
    {
       infile >> c;
       while(c != ' ') // loop till space is found
       {
           count++;
           infile >> c; // get next char
       }
       outfile << count + " "; // write space to keep counts space for reading
       count = 0; // reset counter
    }
    
    infile.close();
    outfile.close();

    return 0; // standard C/C++ exit code of success
}


That code has not been compiled nor tested but should be similar to what you're looking for in respects to how to actually count the number of chars per character and then output that result.



We'll be an army of theives
Of self-freed slaves
Of mild-mannered maids
We'll fight with whispers and blades
So get ready, a new day is dawning
- The New Wild West -- Jewel

Report
Re: how to letters in every word and change to numbers Posted by CplusplusNubie on 20 Apr 2007 at 4:31 PM
: If I understand you correctly you're looking to do something like this?
:
: Say the file contains 3 words such as: One two three
:
: And you want to count them and output that to a file so the output would be to the file: 3 3 5
:
:
: #include <fstream>
: 
: int main(int argc, char** argv)
: {
:     int count = 0;
:     char ch;
:     ifstream infile;
:     ofstream outfile;
: 
:     infile.open("input.txt");
:     outfile.open("output.txt");
: 
:     while(!infile.eof())
:     {
:        infile >> c;
:        while(c != ' ') // loop till space is found
:        {
:            count++;
:            infile >> c; // get next char
:        }
:        outfile << count + " "; // write space to keep counts space for reading
:        count = 0; // reset counter
:     }
:     
:     infile.close();
:     outfile.close();
: 
:     return 0; // standard C/C++ exit code of success
: }

:
: That code has not been compiled nor tested but should be similar to what you're looking for in respects to how to actually count the number of chars per character and then output that result.
:
:

: We'll be an army of theives
: Of self-freed slaves
: Of mild-mannered maids
: We'll fight with whispers and blades
: So get ready, a new day is dawning
: - The New Wild West -- Jewel
:
:

:Yes this is exactly what I was trying to do. Thank you.



 

Recent Jobs