char arr[5000];
ifstream is("test.txt");
is.get(arr,5000);
int i = 0;
int j = 0;
cout << arr << endl;
char anar[5000];
while (arr[i] != '\0')
{
if (arr[i] == 'i' || arr[i] == 'a' || arr[i] == 'e' ||
arr[i] == 'o' || arr[i] == 'u')
{
++i;
}
else anar[j] = arr[i]; ++j; ++i;
}++j; anar[j] = '\0';
cout << anar << endl;
ofstream os("test.txt");
os.write(anar, sizeof(char));
cout << "written successfully" << endl;
should read the data from a file and delete the vowels from this string. After deleting vowels, it should assign the result to another string. But vowels seem strange characters and the writen file is only one character long.
How big do you think sizeof(char) is? So how many characters is this going to write?
os.write(anar, sizeof(char));
You actually have j characters in your array, so this works
os.write(anar, j);
But since you have a null terminated character array even simpler would be
os << anar;
Some other errors, look at this loop
while (arr[i] != '\0')
{
if (arr[i] == 'i' || arr[i] == 'a' || arr[i] == 'e' ||
arr[i] == 'o' || arr[i] == 'u')
{
++i;
}
else anar[j] = arr[i]; ++j; ++i;
}++j; anar[j] = '\0';
It looks like you are missing {} around the else part of the if statement. You also have an extra ++j after the while loop for some reason. Here's how it should look (I think)
while (arr[i] != '\0')
{
if (arr[i] == 'i' || arr[i] == 'a' || arr[i] == 'e' ||
arr[i] == 'o' || arr[i] == 'u')
{
++i;
}
else
{
anar[j] = arr[i];
++j;
++i;
}
}
anar[j] = '\0';
Notice how much easier these problems are to spot if you get into the habit of consistently indenting your code. You should do this.
BTW there are no C++ strings in your code, only character arrays.
A very good answer has been given by john already. So, the problem is solved.
I would like to recommend to you to learn a little bit about C++ and all the existing libraries. Especially the C++ - algorithms library is very powerful.
Look at the below program:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
int main() {
// Open files and check, if they could be opened
if (std::ifstream is("withvowels.txt"); is)
if (std::ofstream os("withoutvowels.txt"); os)
// Copy file and remove vowels
std::copy_if(std::istreambuf_iterator<char>(is), {}, std::ostreambuf_iterator<char>(os), [](const char c) { return !((0x208222 >> (c & 0x1f)) & 1); });
}
So, in essence, we have just 3 statements: 2 times if with initializer. And then one copy_if with a lambda for vowel detection.
If you want to know more about the lambda and vowel detection you can read in one of my other posts here.
EDIT
Op asked, how to read the file into a std::string. I added a new piece of code, where I first read the complete file into a std::string and then erase/remove the vowels. The result is shown on std::cout
Please see:
#include <iostream>
#include <fstream>
#include <algorithm>
#include <iterator>
#include <string>
int main() {
// Open file and check, if it could be opened
if (std::ifstream is("r:\\withvowels.txt"); is) {
// Read the complete file into string variable s
std::string s(std::istreambuf_iterator<char>(is), {});
// Remove all vowels from string
s.erase(std::remove_if(s.begin(), s.end(), [](const char c) { return ((0x208222 >> (c & 0x1f)) & 1); }), s.end());
// Show result
std::cout << s;
}
}
Related
#include <iostream>
using namespace std;
int main() {
char a[101]{0};
cin>>a;
cin.getline(a,101);
cin.ignore();
int currLen{0};
int maxLen{0};
int startInd{-1};
int endInd{-1};
for(int i=0; i<101; i++) {
if(a[i]!=' ' ) {
++currLen;
} else if(a[i]==' '||a[i]=='\0') {
if(currLen>maxLen) {
maxLen=currLen;
startInd=i-currLen;
endInd=i-1;
}
if(a[i]=='\0')
break;
currLen=0;
}
}
cout<<maxLen<<endl;
if(startInd==-1)
cout<<-1;
else
for(int i=startInd; i<=endInd; i++)
cout<<a[i];
return 0;
}
If I take an input here, for example, "My name is Manav Kampani"
It will output 5
Manav instead of 7
Kampani
But if I write "My name is Manav Kampani ", with space after the last word
than it is considering Kampani too printing Kampani.
Also when I input "Kampani Manav is my name" then too it's displaying the wrong output. That means it is not considering the first word of the sentence.
if(a[i]!=' ' )
{
++currLen;
}
else if(a[i]==' '||a[i]=='\0')
{
....
}
Consider the case of a[i] == 0. Which of these if-statements will apply.
Answer: the first one. Which means you'll never look at the final word in the string. You also don't exit at the end of the string, but instead loop through whatever is in your string all the way out to character 101.
As a general structure, be very, very careful with this:
if (condition)
else if (condition)
// without a final else section
If you do that, you need to think about what you're doing. In this particular case, you can have:
if (a[i] != 0 && a[i] != ' ')
else
It may not solve all your issues, but it should solve some.
A nice sliding window pattern implementation.
You have 3 problems in your code
You must not write cin >> a;
You must not write cin.ignore();
You need to modify your if statement like so: if (a[i] != ' ' && a[i] != '\0') Otherwise you will not detect the last word.
Your complete working code with that minor fixes will lokk like that.
int main()
{
char a[101]{ 0 };
//cin >> a;
cin.getline(a, 101);
//cin.ignore();
int currLen{ 0 };
int maxLen{ 0 };
int startInd{ -1 };
int endInd{ -1 };
for (int i = 0; i < 101; i++)
{
if (a[i] != ' ' && a[i] != '\0')// Add comparison
{
++currLen;
}
else if (a[i] == ' ' || a[i] == '\0')
{
if (currLen > maxLen)
{
maxLen = currLen;
startInd = i - currLen;
endInd = i - 1;
}
if (a[i] == '\0')
break;
currLen = 0;
}
}
cout << maxLen << endl;
if (startInd == -1)
cout << -1;
else
for (int i = startInd; i <= endInd; i++)
cout << a[i];
return 0;
}
Additionally. You should not use C-Style arrays in C++. And please use std::string
There is a couple of things here:
1- You don't need to do a cin>>a this is actually consuming the first word, and afterwards the content is overrided by cin.getline(). So removing the firsst cin>>ayou'll be fine.
2- The last word is not read because there isn't any if condition that matches the condition aka.
if(a[i]!=' ' ) case of not a space
//not end of word
else if(a[i]==' '||a[i]=='\0') case of space or null
//end of word
So your last character is not a space nor null, that means you don't detect the last word.
I am trying to create a for loop that has a conditional statement which reads until an operation is found, ex. (+,-,/,*), but every time I try I get an error:
Unhandled exception at 0x7936F2F6 (ucrtbased.dll) in CIS310 Project 44.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
while (getline(infile, hex))
{
n = hex.length();//find the length of the line
for (i = 0; hex[i] != '/'||'+'||'-'||'*'; i++,++k) //loop to split the first hexadecimal number
h1 = h1 + hex[i];
for (i++; i < n - 1; i++) //loop to get the second hexadecimal number
h2 = h2 + hex[i];
n1 = convertDecimal(h1); //convert the first hexadecimal number to decimal
n2 = convertDecimal(h2);
Your condition hex[i] != '/'||'+'||'-'||'*' is malformed. C++ requires that you specify both sides of the operator each time, so you will need something more similar to hex[i] != '/' || hex[i] != '+' || ....
You have to check after every ' | | '(OR), like:
hex[i] != '/' || hex[i] != '+' || hex[i] != '-' || hex[i] != '*'
This is a similar code to what you wrote:
while(getline(file,line))
{
string firstPart = "";
unsigned int i;
//We can use the algorithm library to search for them but its ok
for(i=0;(line[i] != '+') || (line[i] != '-') || (line[i] != '*') || (line[i] != '/') || (line[i] != '\0');i++ );
firstPart = line.substr(0,i);
}
now if you tried this, it will cause the same error (or atleast similar to it), if we even try to print every character in the loop
for(/*stuff*/)
cout << line[i];
Then notice this will become an infinite loop, the problem is that you're checking the character line[i] if it wasn't a + or - or * or / all at the same time, fix this by changing the || to &&.
I'll suppose that your file (named testfile.txt) has the content below:
0xAB+0xCD
0x11-0x03
Sample working code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream file("testfile.txt");
///Don't forget to check if file has opened
if(!file.is_open())
{
cout << "File didn\'t open :(";
return 0;
}
string line;
while(getline(file,line))
{
string firstPart = "",secondPart = "";
char operation;
unsigned int i;
//We can use the algorithm library to search for them but its ok
for(i=0;(line[i] != '+') && (line[i] != '-') && (line[i] != '*') && (line[i] != '/') && (line[i] != '\0');i++ );
firstPart = line.substr(0,i);
operation = line[i];
secondPart = line.substr(i+1,firstPart.size());
}
file.close();
return 0;
}
The below program is intended to make all characters in a string lowercase, remove all vowels, and then print a full stop before every letter. For example, an input of "umbrella" would become ".m.b.r.l.l". But when I input "tour", the 'u' is not removed.
char ChangeToLow(char letter) {
if(letter <= 'Z' && letter >= 'A')
return letter - ('A' - 'a');
return letter;
}
int main()
{
string name;
cin>>name;
for (int i = 0 ; i < name.length() ; i++)
{
name[i] = ChangeToLow(name[i]);
if (name[i] == 'y' || name[i] == 'a'|| name[i] == 'u'|| name[i] == 'i'|| name[i] == 'e'|| name[i] == 'o')
{
name.erase(i,1);
}
}
for (int i = 0 ; i < name.length() ; i++)
{
cout<<'.'<<name[i];
}
}
I expect the output ".t.r" but instead it prints ".t.u.r".
Thanks in advance.
When you erase a character from the string, the remaining contents move over to fill the space. Their indexes adjust accordingly. With your tour example, it'll look something like the following:
Your loop counter, i, was incremented to 2 after you deleted the 'o' from tour, and name[i] is now 'r'. One option to avoid this behavior is to decrement i when you delete a vowel.
I have some suggestion for you. Firstly you shouldn't put using namespace std; in your code. It just adds confusion and is considered a bad practice. I think it would be also a good thing, if you would consider to learn the STL, if it is really your aim to learn C++ in depths. As for the error I think that the already posted answer shows your wrong assumptions.
#include <iostream>
#include <cstdlib>
#include <locale>
#include <set>
#include <algorithm>
int main()
{
std::string name;
std::cin>>name;
std::set<char> vowels={'a','u','i','e','o'};
std::transform(name.begin(), name.end(), name.begin(), [](auto v){ return std::tolower(v, std::locale());});
auto iter=std::remove_if(name.begin(), name.end(), [&vowels](auto v){ return vowels.find(v)!=vowels.end();});
name.erase(iter,name.end());
for (int i = 0 ; i < name.length() ; i++)
{
std::cout<<'.'<<name[i];
}
return EXIT_SUCCESS;
}
I'm new to programming, so I created number guessing game which worked perfectly but I can't seem to finish with the word guessing code. My goal is to print "Congratulations" when the guessed string is correct, but I tried many ways and I still can't make it work.
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int i;
const string wordList[17] = { "television",
"computer", "keyboard", "laptop", "mouse", "phone", "headphones",
"screen", "camera", "sound", "science", "programming",
"entertainment",
"graphics", "intelligent", "memory", "remote" };
string word = wordList[rand() % 17];
for(i = 0; i < word.length(); i++)
{
if(word[i] == 'a' || word[i] == 'e' || word[i] == 'i' ||
word[i] == 'o' || word[i] == 'u')
{
word[i] = '_';
}
}
cout << word << endl;
int n=0;
string x;
do
{
n++;
cin >> x;
}
while(x!=word[i]);
cout<<"Congratulations! You guessed the word!";
return 0;
}
I'd say most of your problems come down to this line:
while(x!=word[i]);
As the comments suggest, word is your modified word, not the word list. But also, i is the wrong index. So save the word index you chose earlier:
size_t wordIndex = rand() % 17;
string word = wordList[wordIndex];
Then change your do loop condition:
while (x != wordList[wordIndex]);
I'd also recommend that you don't use using namespace std;.
You could argue about the use of rand(), but it might not be worth it. Just be aware that rand() has shortcomings and better alternatives exist.
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
main()
{
string str1;
char strArray[80];
cout << "Enter string: ";
getline(cin, str1);
transform(str1.begin(), str1.end(), str1.begin(), ::tolower);
for(int i = 0;i < str1.length();i++)
{
if(str1[i] == ' ' || str1[i] == ',' || str1[i] == '.')
{
}
else
{
strArray[i] = str1[i];
}
}
cout << strArray;
return 0;
}
The for loop keeps stopping after it finds a space, comma, or period. Could someone explain to me why this is happening?
The problem is that i keeps incrementing even though you erased a character from the input. It's not actually stopping, just skipping a character. Since strArray now has a hole in it, it's likely that the hole is filled with 0 thus ending the C-string. P.S. this behavior is not guaranteed and you might end up with completely different results on another run of the program.
When you call erase it affects the container, so you need to handle this. erase() returns the iterator to the next element after the deleted one, so you should use that instead:
int i = 0;
for(string::iterator it = str1.begin(); it != str1.end(); )
{
if(*it == ' ' || *it == ',' || *it == '.')
{
it = str1.erase(it);
}
else
{
strArray[i++] = *it++;
}
}
strArray[i] = '\0'; // terminate string
Can you post which string you input for str1? Cause I try run it and it run well without stop. The only proplem I found with your code is that you erase the char in the loop which will lead to wrong result string.
You are erasing characters from the string while still continuing to increment the counter. Remove ++i from the for loop. Put it under the else clause.
for(int i = 0;i < str1.length();)
{
if(str1[i] == ' ' || str1[i] == ',' || str1[i] == '.')
{
str1.erase(i, 1);
}
else
{
strArray[i] = str1[i];
++i;
}
}
strArray[str1.length()] = '\0';