Hello,

I am working on a script that reads each individual character from a txt file and then outputs it depending on whether it's in the alphabet or not.. The problem is, it won't output spaces or anything apart from the string..

e.g.
The text file reads:

Hello, my name is Phorce and this is a test script.

and the output is:

hellomynameisphorceandthisisatestscript

and here is the .cpp file:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char alphabet[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
    char alphabetU[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
    char characters[400];
    char inputted[400];
    int sizeinputted = sizeof(inputted)/sizeof(inputted[0]);
    ifstream file("input.txt");
    
    if(file)
    {
        char words[447];
        for(int i=0; !file.eof(); i++)
		{
            words[i] = file.get();
            for(int o=0; (o < 26); o++)
            {
                if(words[i] == alphabet[o] || words[i] == alphabetU[o])
		   		{
                    inputted[i] = alphabet[o];
                    
                }
                
            }
        }
        
        for(int i=0; (i < sizeinputted); i++)
        {	
            cout << inputted[i];
        }
    }else{
        
        cout << "Cannot open file";
        
    }
    
    
    return EXIT_SUCCESS;
}

Anyone have any ideas? Thanks =)

Recommended Answers

All 4 Replies

You can add the punctuation to your alphabet OR you can have it eliminate things that are NOT alphabet or punctuation.

Hey, thanks for your reply.. Do you mean like:

char alphabet[28] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z', ',' '.' };

??

Also, how would I get around spaces.. that's the main issue i'm having!

I think there's a char code that you can use. (like /20?)

I think there's a char code that you can use. (like /20?)

Blank space IS a char.

#include <iostream>
using std::cout;

int main()
{
 char c = ' ';
 cout <<"Space" << c << "is" <<c <<"a"<<c<<"Character.";
 cout <<"\nAscii code = " << int(c);
 return 0;
}

Note the space between words.

If you specify blank space, OP, then you should be good to go, though I suggest using the int values instead, it will look much cleaner.

ASCII Values

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.