Basically I have a file with a fixed amount of words and I want to go through and identify all the words with 9 characters and randomly remove one character. Any help?
Well show us what you've got first. I am willing to help, but I am not writing code for you.
i'm guessing you'll be storing your words in an array of strings (strings being arrays of chars)
so you'd have "char words[16][20];" that stores 20 words with a max length of 15 characters (16th spot is the null byte to end your string)
the easiest way to check the length of 9 is to check place 8 (counting starts at 0) for null bytes, every one thats not a null byte on place 8 gets tested for null bytes on place 9, the ones that have a null byte there are 9 characters long, and go to the next step.
to stop flaws from happening due to random data in the array i suggest filling it with null bytes before you fill it with words.
the randomly removing one char is as simple as generating a random number between 0 and 8, and copying every character after that place, until the first null byte, one index lower (if 5 is picked 6 goes to 5, 7 to 6 etc, until 9 -null byte- goes to 8, shortening the string)
as for the file thing, its a matter of reading each word seperately, and placing it in the array accordingly, and if writing back is necessary, i suggest just wiping the file, and writing the array to it.
sidenote: i know this is probably not a cpu/ram efficient way of doing things, but it should get the job done in a fairly easy way,
and if anyone spots any logic errors, feel free to correct me.
For reading into and writing I have:
#include <string.h>
void readFromFile(FILE *inFile) {
FILE * files[9];
inFile = fopen("words.txt", "r");
file[0] = fopen("file1.out", "rw+");
file[1] = fopen("file2.out", "rw+");
file[2] = fopen("file3.out", "rw+");
file[3] = fopen("file4.out", "rw+");
file[4] = fopen("file5.out", "rw+");
file[5] = fopen("file6.out", "rw+");
file[6] = fopen("file7.out", "rw+");
file[7] = fopen("file8.out", "rw+");
file[8] = fopen("file9.out", "rw+");
}
This project goes all the way to 1 character but I just need help getting started persay.
removeChar(String s, char c) {
String r = "";
int i = 0
for (i = 0; i < strlen(s); i ++) {
if (s.[i] != c) r += s.[i];
}
return r;
}
I was thinking something like this could remove a character. Not sure though.
Bad formatting is the site's fault.
guys how in the world can i post code here? i have tried
and <code> </code>. none of them seem to work.
It is because their text filter on the forum reads the '<' and '>' as html > and <. You have to switch to 'Plain text' mode to actually edit the html tags. It would be a lot more convenient if they just made it automatic though!
here is something to get you started
#include <stdio.h> #include <stdlib.h> #include <string.h>
this whole thing has to be in a loop that stops when you reach the last word in the text file. Do you know the number of words that is going to be in the text file?
int main()
{
File * fp = fopen("file.txt","r+"); // opining a file and making a pointer to it
int random = rand() %10; // generate a random number between 0-9
char * word; // creating a string for the word that you are scanning
fscanf(fp,"%s",word)
if(strlen(word) == 9){ // checking if the word has 9 characters
//now do the stuff to remove the random character which number is held in random
}
return 0;
}
I gave you a way to scan a word from the txt file and point a pointer to it. check if the word has 9 character, then you have to fill in the part where you remove the random character. generating a random number between 0-9 will till you which character to remove
good luck
Well that was hard to deal with. Maybe copy that long line into your IDE and it will correct it.
First of all thanks for your valuable articles and then I would request you to be regular with more helpful information. I would love to visit more often.
Thanks for the help, I actually turned the homework in Tuesday night.
Did it work? Not really but neither did anyone else's. Even the professor said he probably couldn't do it in a week. This class sucks so bad haha.