How to count word occurrences each different word in C - c

Program should include the words in the table in the same order in
which they appear in the text.
Use string.h, ctype.h, stdio.h, include strtok function
#include<ctype.h>
int main(void)
{
int i,j;
char text[3][80];
char wordList[120][80];
int count = 0;
char* ptr;
for (i = 0; i <= 2; i++) {
gets(&text[i][0]);
}
for (i = 0; i <= 2; i++) {
for (j = 0; text[i][j]!='\0' ; j++) {
text[i][j] = tolower(text[i][j]);
}
}
ptr = strtok(text, " ,.;:!?-()[]<>");
while (ptr != NULL) {
}
I've been thinking for a long time, and I don't know how to try. You could ask me what's wrong with my code, but I don't know the approach at all...

try this...
#include <stdio.h>
#include <string.h>
void main()
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char str[100], p[50][100], str1[20], ptr1[50][100];
char *ptr;
printf("Enter the string\n");
scanf(" %[^\n]s", str);
for (i = 0;i<strlen(str);i++)
if ((str[i] == ' ')||(str[i] == ',' && str[i+1] == ' ')||(str[i] == '.'))
space++;
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
{
p[i][k] = '\0';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
printf("%s -> %d times\n", ptr1[i], c);
c = 0;
}
}

try this
#include <stdio.h>
#include <string.h>
void main()
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char str[100], p[50][100], str1[20], ptr1[50][100];
char *ptr;
printf("Enter the string\n");
scanf(" %[^\n]s", str);
for (i = 0;i<strlen(str);i++)
{
if ((str[i] == ' ')||(str[i] == ',' && str[i+1] == ' ')||(str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
{
p[i][k] = '\0';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
printf("%s -> %d times\n", ptr1[i], c);
c = 0;
}
}

Related

Backtracking crossword algorithm in c

So i made a backtracking algorithm in c, where i get a txt file filled with words in each line and another txt file just has coordinates for the black squares of a square crossword. I know my implementation is extremelly simple, but i just want to have the barebones algorithm, to make it much faster later on with constraint satisfaction, forward checking etc.
However i have no idea how to get the basic code to work without segmentation faults and wrong solutions! Ant advice would be very helpful!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_WORDS 150000
#define MAX_DIMENSION 200
#define MAX_BLACK_SQUARES 250
int is_valid_word(char *word, int dimension, char crossword[MAX_DIMENSION][MAX_DIMENSION])
{
int i, j, k;
int word_length = strlen(word);
// check if word is too long
if (word_length > dimension)
{
return 0;
}
// check if word overlaps existing letters
for (i = 0; i < dimension; i++)
{
for (j = 0; j < dimension; j++)
{
if (crossword[i][j] != '_')
{
for (k = 0; k < word_length; k++)
{
if ((i + k < dimension && crossword[i + k][j] != '_' && crossword[i + k][j] != word[k]) ||
(j + k < dimension && crossword[i][j + k] != '_' && crossword[i][j + k] != word[k]))
{
return 0;
}
}
}
}
}
return 1;
}
int solve_crossword(int dimension, char crossword[MAX_DIMENSION][MAX_DIMENSION], char words[MAX_WORDS][30], int num_words, int word_index)
{
int i, j, k;
if (word_index >= num_words)
{
// all words have been placed, so return true
return 1;
}
// try placing the current word horizontally
for (i = 0; i < dimension; i++)
{
for (j = 0; j < dimension - strlen(words[word_index]); j++)
{
if (crossword[i][j] != '#')
{
// try placing the word here
int is_valid = 1;
for (k = 0; k < strlen(words[word_index]); k++)
{
if (crossword[i][j + k] != '_' && crossword[i][j + k] != words[word_index][k])
{
is_valid = 0;
break;
}
}
if (is_valid)
{
// place the word
for (k = 0; k < strlen(words[word_index]); k++)
{
crossword[i][j + k] = words[word_index][k];
}
// recursive call to place the next word
if (solve_crossword(dimension, crossword, words, num_words, word_index + 1))
{
return 1;
}
// backtrack by removing the word
for (k = 0; k < strlen(words[word_index]); k++)
{
crossword[i][j + k] = '_';
}
}
}
}
}
// try placing the current word vertically
for (i = 0; i < dimension - strlen(words[word_index]); i++)
{
for (j = 0; j < dimension; j++)
{
if (crossword[i][j] != '#')
{
// try placing the word here
int is_valid = 1;
for (k = 0; k < strlen(words[word_index]); k++)
{
if (crossword[i + k][j] != '_' && crossword[i + k][j] != words[word_index][k])
{
is_valid = 0;
break;
}
}
if (is_valid)
{
// place the word
for (k = 0; k < strlen(words[word_index]); k++)
{
crossword[i + k][j] = words[word_index][k];
}
// recursive call to place the next word
if (solve_crossword(dimension, crossword, words, num_words,word_index + 1))
{
return 1;
}
// backtrack by removing the word
for (k = 0; k < strlen(words[word_index]); k++)
{
crossword[i + k][j] = '_';
}
}
}
}
}
// if we get here, we were unable to place the word in either direction
return 0;
}
int main()
{
// open dictionary file
FILE *dict_file = fopen("dicts/EvenMoreWords.txt", "r");
if (dict_file == NULL)
{
printf("Unable to open dict.txt\n");
return 1;
}
// read dictionary into array
char words[MAX_WORDS][30];
int num_words = 0;
char buffer[30];
while (fgets(buffer, 30, dict_file) != NULL)
{
int word_length = strlen(buffer);
if (buffer[word_length - 1] == '\n')
{
buffer[word_length - 1] = '\0';
}
strcpy(words[num_words], buffer);
num_words++;
}
fclose(dict_file);
// open crossword file
FILE *crossword_file = fopen("crosswords/c1.txt", "r");
if (crossword_file == NULL)
{
printf("Unable to open crossword.txt\n");
return 1;
}
// read dimensions and black squares
int dimension, i, j, width, height;
fscanf(crossword_file, "%d", &dimension);
// initialize crossword
char crossword[MAX_DIMENSION][MAX_DIMENSION];
while (fscanf(crossword_file, "%d %d", &width, &height) != EOF)
{
crossword[width - 1][height - 1] = '#';
}
printf("\n\n\n");
for (i = 0; i < dimension; i++)
{
for (j = 0; j < dimension; j++)
{
if (crossword[i][j] != '#')
{
crossword[i][j] = '_';
printf("%c ", crossword[i][j]);
}
else
{
printf("%c ", crossword[i][j]);
}
}
printf("\n");
}
printf("\n\n\n");
// solve crossword
if (solve_crossword(dimension, crossword, words, num_words, 0))
{
// print solution
for (i = 0; i < dimension; i++)
{
for (j = 0; j < dimension; j++)
{
printf("%c ", crossword[i][j]);
}
printf("\n");
}
}
else
{
printf("Unable to find a solution\n");
}
return 0;
}

Not sure why int value is not what it is supposed to be

I'm solving this problem : https://www.hackerrank.com/challenges/structuring-the-document/problem
When I run my program on my IDE (XCode) I can see that word_count int 7428912 is not what it is supposed to be for any input. I am not sure why. I know that I am accessing out of bounds array index but I need someone to show me where exactly. The program outputs correctly and then gives an error. Thread 1: EXC_BAD_ACCESS (code=1, address=0x73696870)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_CHARACTERS 1005
#define MAX_PARAGRAPHS 5
struct word {
char* data;
};
struct sentence {
struct word* data;
int word_count;//denotes number of words in a sentence
};
struct paragraph {
struct sentence* data ;
int sentence_count;//denotes number of sentences in a paragraph
};
struct document {
struct paragraph* data;
int paragraph_count;//denotes number of paragraphs in a document
};
#include <ctype.h>
struct document get_document(char* text) {
int spaces = 0, periods = 0, newlines = 0;
for(int i = 0; i < strlen(text); i++)
if(text[i] == ' ')
spaces++;
else if(text[i] == '.')
periods++;
else if(text[i] == '\n')
newlines++;
struct document doc;
doc.paragraph_count = newlines + 1;
doc.data = malloc((newlines + 1) * sizeof(struct paragraph));
int inBetweenPeriods = 0, j = 0;
struct paragraph para[doc.paragraph_count];
for(int i = 0; i < doc.paragraph_count; i++) {
for(; j < strlen(text); )
if(text[j] == '.') {
inBetweenPeriods++;
j++;
}
else if(text[j] == '\n' || j == strlen(text) - 1) {
para[i].sentence_count = inBetweenPeriods;
j++;
break;
}
else
j++;
para[i].data = malloc((inBetweenPeriods) * sizeof(struct sentence));
inBetweenPeriods = 0;
}
struct sentence sen[periods];
int sp[periods];
for(int j = 0; j < periods; j++)
sp[j] = 0;
int beg = 0;
int ij = 0;
for(int j = 0; j < strlen(text); j++) {
if(text[j] == '.') {
for(int k = beg; k < j; k++)
if(text[k] == ' ')
sp[ij]++;
ij++;
beg = j + 1;
}
}
for(int i = 0; i < periods; i++) {
sen[i].word_count = sp[i] + 1;//spaces + 1;
sen[i].data = malloc((sp[i] + 1) * sizeof(struct word));
}
struct word word[spaces + periods];
int start = 0, k = 0, wordsub = 0, sensub = 0, parasub = 0, docsub = 0, wordno = 0, parano = 0;
for(int i = 0; i < strlen(text); i++) {
if(text[i] == ' ' || text[i] == '.') {
word[wordsub].data = malloc((i - start) * sizeof(char) + 1);
for(int j = start; j < i; j++)
word[wordsub].data[k++] = text[j];
word[wordsub].data[k++] = '\0';
k = 0;
if(i < strlen(text) - 1 && text[i + 1] == '\n')
start = i + 2;
else
start = i + 1;
if(text[i] == ' ') {
sen[sensub].data[wordno] = word[wordsub];
wordno++; //wordno can be 0 or 1
}
if(i != strlen(text) - 1 && isalpha(text[i + 1]) && text[i] == '.') {
sen[sensub].data[wordno] = word[wordsub];
wordno = 0;
para[parasub].data[parano] = sen[sensub];
sensub++;
parano++;
}
if( (i != strlen(text) - 1 && text[i + 1] == '\n') || i == strlen(text) - 1) {
sen[sensub].data[wordno] = word[wordsub];
wordno = 0;
para[parasub].data[parano++] = sen[sensub];
parano = 0;
doc.data[docsub++] = para[parasub];
parasub++;
sensub++;
}
wordsub++;
}
}
return doc;
}
struct word kth_word_in_mth_sentence_of_nth_paragraph(struct document Doc, int k, int m, int n) {
return Doc.data[n - 1].data[m - 1].data[k - 1];
}
struct sentence kth_sentence_in_mth_paragraph(struct document Doc, int k, int m) {
return Doc.data[m - 1].data[k - 1];
}
struct paragraph kth_paragraph(struct document Doc, int k) {
return Doc.data[k - 1];
}
void print_word(struct word w) {
printf("%s", w.data);
}
void print_sentence(struct sentence sen) {
for(int i = 0; i < sen.word_count; i++) {
print_word(sen.data[i]);
if (i != sen.word_count - 1) {
printf(" ");
}
}
}
void print_paragraph(struct paragraph para) {
for(int i = 0; i < para.sentence_count; i++){
print_sentence(para.data[i]);
printf(".");
}
}
void print_document(struct document doc) {
for(int i = 0; i < doc.paragraph_count; i++) {
print_paragraph(doc.data[i]);
if (i != doc.paragraph_count - 1)
printf("\n");
}
}
char* get_input_text() {
int paragraph_count;
scanf("%d", &paragraph_count);
char p[MAX_PARAGRAPHS][MAX_CHARACTERS], doc[MAX_CHARACTERS];
memset(doc, 0, sizeof(doc));
getchar();
for (int i = 0; i < paragraph_count; i++) {
scanf("%[^\n]%*c", p[i]);
strcat(doc, p[i]);
if (i != paragraph_count - 1)
strcat(doc, "\n");
}
char* returnDoc = (char*)malloc((strlen (doc)+1) * (sizeof(char)));
strcpy(returnDoc, doc);
return returnDoc;
}
int main()
{
char* text = get_input_text();
struct document Doc = get_document(text);
int q;
scanf("%d", &q);
while (q--) {
int type;
scanf("%d", &type);
if (type == 3){
int k, m, n;
scanf("%d %d %d", &k, &m, &n);
struct word w = kth_word_in_mth_sentence_of_nth_paragraph(Doc, k, m, n);
print_word(w);
}
else if (type == 2) {
int k, m;
scanf("%d %d", &k, &m);
struct sentence sen= kth_sentence_in_mth_paragraph(Doc, k, m);
print_sentence(sen);
}
else{
int k;
scanf("%d", &k);
struct paragraph para = kth_paragraph(Doc, k);
print_paragraph(para);
}
printf("\n");
}
}
I solved it like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX_CHARACTERS 1005
#define MAX_PARAGRAPHS 5
struct word {
char* data;
};
struct sentence {
struct word* data;
int word_count;//denotes number of words in a sentence
};
struct paragraph {
struct sentence* data ;
int sentence_count;//denotes number of sentences in a paragraph
};
struct document {
struct paragraph* data;
int paragraph_count;//denotes number of paragraphs in a document
};
#include <ctype.h>
struct document get_document(char* text) {
int spaces = 0, periods = 0, newlines = 0;
for(int i = 0; i < strlen(text); i++)
if(text[i] == ' ')
spaces++;
else if(text[i] == '.')
periods++;
else if(text[i] == '\n')
newlines++;
struct document doc;
doc.paragraph_count = newlines + 1;
doc.data = malloc((newlines + 1) * sizeof(struct paragraph));
int inBetweenPeriods = 0, j = 0;
struct paragraph para[doc.paragraph_count];
for(int i = 0; i < doc.paragraph_count; i++) {
for(; j < strlen(text);)
if(text[j] == '.')
{ inBetweenPeriods++;j++;}
else if(text[j] == '\n' || j == strlen(text) - 1)
{j++;break;}else j++;
para[i].sentence_count = inBetweenPeriods;
para[i].data = malloc((inBetweenPeriods) * sizeof(struct sentence));
inBetweenPeriods = 0;
}
struct sentence sen[periods];
int sp[periods];
for(int j = 0; j < periods; j++)
sp[j] = 0;
int beg = 0;
int ij = 0;
for(int j = 0; j < strlen(text); j++) {
if(text[j] == '.') {
for(int k = beg; k < j; k++)
if(text[k] == ' ')
sp[ij]++;
ij++;
beg = j + 1;
}
}
for(int i = 0; i < periods; i++) {
sen[i].word_count = sp[i] + 1;//spaces + 1;
sen[i].data = malloc((sp[i] + 1) * sizeof(struct word));
}
struct word word[spaces + periods];
int start = 0, k = 0, wordsub = 0, sensub = 0, parasub = 0, docsub = 0, wordno = 0, parano = 0;
for(int i = 0; i < strlen(text); i++) {
if(text[i] == ' ' || text[i] == '.') {
word[wordsub].data = malloc((i - start) * sizeof(char) + 1);
for(int j = start; j < i; j++)
word[wordsub].data[k++] = text[j];
word[wordsub].data[k++] = '\0';
k = 0;
if(i < strlen(text) - 1 && text[i + 1] == '\n')
start = i + 2;
else
start = i + 1;
if(text[i] == ' ') {
sen[sensub].data[wordno] = word[wordsub];
wordno++; //wordno can be 0 or 1
}
if(i != strlen(text) - 1 && isalpha(text[i + 1]) && text[i] == '.') {
sen[sensub].data[wordno] = word[wordsub];
wordno = 0;
para[parasub].data[parano] = sen[sensub];
sensub++;
parano++;
}
if( (i != strlen(text) - 1 && text[i + 1] == '\n') || i == strlen(text) - 1) {
sen[sensub].data[wordno] = word[wordsub];
wordno = 0;
para[parasub].data[parano++] = sen[sensub];
parano = 0;
doc.data[docsub++] = para[parasub];
parasub++;
sensub++;
}
wordsub++;
}
}
return doc;
}
struct word kth_word_in_mth_sentence_of_nth_paragraph(struct document Doc, int k, int m, int n) {
return Doc.data[n - 1].data[m - 1].data[k - 1];
}
struct sentence kth_sentence_in_mth_paragraph(struct document Doc, int k, int m) {
return Doc.data[m - 1].data[k - 1];
}
struct paragraph kth_paragraph(struct document Doc, int k) {
return Doc.data[k - 1];
}
void print_word(struct word w) {
printf("%s", w.data);
}
void print_sentence(struct sentence sen) {
for(int i = 0; i < sen.word_count; i++) {
print_word(sen.data[i]);
if (i != sen.word_count - 1) {
printf(" ");
}
}
}
void print_paragraph(struct paragraph para) {
for(int i = 0; i < para.sentence_count; i++){
print_sentence(para.data[i]);
printf(".");
}
}
void print_document(struct document doc) {
for(int i = 0; i < doc.paragraph_count; i++) {
print_paragraph(doc.data[i]);
if (i != doc.paragraph_count - 1)
printf("\n");
}
}
char* get_input_text() {
int paragraph_count;
scanf("%d", &paragraph_count);
char p[MAX_PARAGRAPHS][MAX_CHARACTERS], doc[MAX_CHARACTERS];
memset(doc, 0, sizeof(doc));
getchar();
for (int i = 0; i < paragraph_count; i++) {
scanf("%[^\n]%*c", p[i]);
strcat(doc, p[i]);
if (i != paragraph_count - 1)
strcat(doc, "\n");
}
char* returnDoc = (char*)malloc((strlen (doc)+1) * (sizeof(char)));
strcpy(returnDoc, doc);
return returnDoc;
}
int main()
{
char* text = get_input_text();
struct document Doc = get_document(text);
int q;
scanf("%d", &q);
while (q--) {
int type;
scanf("%d", &type);
if (type == 3){
int k, m, n;
scanf("%d %d %d", &k, &m, &n);
struct word w = kth_word_in_mth_sentence_of_nth_paragraph(Doc, k, m, n);
print_word(w);
}
else if (type == 2) {
int k, m;
scanf("%d %d", &k, &m);
struct sentence sen= kth_sentence_in_mth_paragraph(Doc, k, m);
print_sentence(sen);
}
else{
int k;
scanf("%d", &k);
struct paragraph para = kth_paragraph(Doc, k);
print_paragraph(para);
}
printf("\n");
}
}

Read second Line of File in C

So, I programmed a Cesar Encryption that saves the Position of "Space" Characters in the Second Line of the encryptet File with Numbers (like that:
HMEQSRHLMIV //Encryptet Text (here is Enter normally)
7 //Number where the Space Char is
)
My Problem now is, that when I read the String with fgets(), but it only reads until \n, so how do i get the second Line of the File?
here is my code if that helps.
#include <stdio.h>
#include <stdbool.h>
#define asize 80
void deleteEnter(char[]);
void decrypt(char[], int, char[]);
int main() {
char Message[asize] = {0};
int code = 0;
char encMessageFile[asize + 4] = {};
char encMessage[asize] = {};
char MessageFile[asize] = {};
FILE *fp;
FILE *fp2;
bool open = false;
printf("Gib die verschluesselte Datei an: \t");
fgets(encMessageFile,asize + 4, stdin);
deleteEnter(encMessageFile);
fp = fopen(encMessageFile, "r");
do {
if (fp == NULL) {
printf("\nEs wurde keine Datei gefunden die so heisst\n");
open = false;
fclose(fp);
} else {
open = true;
}
} while (open == false);
fgets(encMessage,asize,fp);
printf("\nGib den Code zum entschluesseln ein: \t");
scanf("%i", &code);
decrypt(encMessage, code, Message);
for (int i = 0; i < asize + 4; ++i) {
if (encMessageFile[i] == '.' && encMessageFile[i + 1] == 'e' && encMessageFile[i + 2] == 'n' && encMessageFile[i + 3] == 'c' && encMessageFile[i + 4] == '\0') {
for (int j = 0; j < i; j++) {
if (encMessageFile[j] != '\0') {
MessageFile[j] = encMessageFile[j];
MessageFile[j + 1] = '\0';
}
}
i = asize + 4;
}
}
fp2 = fopen(MessageFile, "w");
fputs(Message, fp2);
fclose(fp2);
fclose(fp);
remove(encMessageFile);
}
void deleteEnter(char encMessage[]){
for (int i = 0; i < asize; i++) {
if (encMessage[i] == '\0') {
i = asize;
} else if (encMessage[i] == '\n'){
encMessage[i] = '\0';
i = asize;
}
}
}
void decrypt(char encMessage[], int code, char Message[]) {
int a = code % 26;
int LeerPos[asize] = {0};
for (int k = 0; k < asize; k++) {
if (encMessage[k] == '\n'){
for (int i = 0; i < asize; ++i) {
if (encMessage[k + i] != '\0') {
LeerPos[i] = encMessage[k + i] - 80;
} else {
i = asize;
}
}
encMessage[k] = '\0';
}
}
for (int i = 0; i < asize; i++) {
if (encMessage[i] == '\0') {
i = asize;
} else {
for (int j = 0; j < a; j++) {
encMessage[i] = encMessage[i] - 1;
if (encMessage[i] < 'A' && encMessage[i] > 'A' - 2) {
encMessage[i] = 'Z';
}
}
}
}
int x = 0;
for (int l = 0; l < asize; l++) {
for (int i = 0; i < asize; i++) {
if (l == LeerPos[i]) {
Message[l] = ' ';
x++;
} else {
Message[l] = encMessage[l - x];
}
}
}
}
I expect, that the Program after the Decryption the Message is "DIAMOND HIER"
Because a User wantet that I post the Encryption too, here it is:
#include <stdio.h>
#include <stdbool.h>
#define asize 80
void cleanup (char[],char[], int[]);//2 arrays input und endinput
void deleteEnter(char[]);
void toupper(char[]); //endinput
int laenge(char[]); //input pointeranzleer
void encrypt(char[], int);
void Ausgabe(char[], char[], int[]);
//i abspeichern. for schleife in dritte array und array+1 und i++
int main() {
FILE *fp;
int schluessel=0;
char input[asize]={' '};
char endinput[asize]={' '};
char name[asize] = {' '};
bool open = false;
int LeerPos[80] = {0};
do {
printf("Bitte geben Sie den Namen der Datei ein:");
fgets(name, asize, stdin);
deleteEnter(name);
fp = fopen(name, "r");
if (fp == NULL) {
printf("\nEs wurde keine Datei gefunden die so heisst\n");
open = false;
fclose(fp);
} else {
open = true;
}
} while (open == false);
fgets(input, asize, fp);
printf("Bitte geben sie den Schluessel ein:");
scanf("%d",&schluessel);
cleanup(input,endinput, LeerPos);
toupper(endinput);
encrypt(endinput, schluessel);
Ausgabe(endinput, name, LeerPos);
fclose(fp);
return 0;
}
int laenge(char input[asize]){
int i=0;
while(input[i]!='\0'){
i++;
}
return i;
}
void cleanup(char input[asize],char endinput[asize], int LeerPos[]){
int j=0;
int LC = 0;
int lengthIn = laenge(input);
for(int i=0;i<lengthIn;i++){
if(input[i]!=' '){
endinput[j]=input[i];
j++;
} else {
LeerPos[LC] = i;
LC++;
}
}
}
void toupper(char endinput[asize]){
for(int i=0;i<laenge(endinput);i++){
if(endinput[i]>='a'&&endinput[i]<='z'){
endinput[i]=endinput[i]-('z'-'Z');
}
}
}
void encrypt(char S[asize], int code) {
int a = code % 26;
for (int i = 0; i < 80; i++) {
if (S[i] == '\0') {
i = 80;
} else {
for (int j = 0; j < a; j++) {
S[i] = S[i]+1;
if (S[i] == 'Z' + 1) {
S[i] = 'A';
}
}
}
}
}
void Ausgabe(char S[asize], char name[asize], int LeerPos[]) {
FILE *fpOut;
char EName[asize + 4];
char EncEnd[4] = {'.','e','n','c'};
for (int j = 0; j < asize; j++) {
EName[j] = name[j];
}
for (int k = 0; k < asize + 4; k++) {
if (EName[k] == '\0'){
for (int i = k; i < k + 4; ++i) {
EName[i] = EncEnd[i - k];
}
k = asize+4;
}
}
fpOut = fopen(EName, "w");
printf("\nVerschluesselte File: %s", EName);
for (int i = 0; i < asize; i++) {
if (S[i] == '\0') {
i = asize;
}else {
fprintf(fpOut, "%c", S[i]);
}
}
fprintf(fpOut, "%c", '\n');
for (int l = 0; l < asize; l++) {
if (LeerPos[l] != '\0'){
fprintf(fpOut, "%i", LeerPos[l]);
} else {
l = asize;
}
}
remove(name);
fclose(fpOut);
}
void deleteEnter(char S[]) {
for (int i = 0; i < asize ; i++) {
if (S[i] == '\0') {
i = asize;
} else if (S[i] == '\n') {
S[i] = '\0';
}
}
}
Thanks for all the Help.
Arigatou Gozaimasu

Counting the occurrence of words in a file

My wordOccurrences don't work if you read a file. They can appear twice in the counting so it doesn't count correctly but if I input from stdin it counts correctly.
So I have to read in a file (-i input.txt) count the words and word occurrences in that file. Output the results in the specific file that is given with -o output.txt. If there is a -c it should ignore punctuation and convert to lowercase
MAIN
#include "count.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
//#include "wordOccurances.h"
#include "wordOccurrences.c"
int main(int argc, char **argv)
{
//Initialize variables
FILE *fi; // input file
FILE *fo =stdout; //output file
char buffer[1000];
char *input; //for manually entering string with -c
input = (char*)malloc(100 * sizeof(char));
char *name = "invalid";
int wordcount; // the number of words
char *ch; //a single character
ch = (char*)malloc(100000 * sizeof(char));
int c = 0, i, iFlag = 0, oFlag = 0, cFlag = 0;
char ptr1[50][100];
char *ptr;
if(argc == 1)
{
printf("Default settings\n");
}
else
{
for(i=1; i<argc;i++)
{
if(strcmp(argv[i], "-i")==0)
{
printf("input\n");
iFlag = 1;
fi = fopen(argv[++i],"r");
}
if(strcmp(argv[i], "-o")==0)
{
printf("output\n");
oFlag = 1;
fo = fopen(argv[++i],"w");
}
if(strcmp(argv[i], "-c")==0)
{
cFlag = 1;
}
}
}
if(iFlag ==1)
{
wordcount = countForFile(fi, wordcount);
fprintf(fo,"Word count is: %d \n", wordcount);
wordOccurencesForFile(fi, cFlag,fo);
}
else
{
printf("Enter text: ");
scanf(" %[^\n]s", input);
//Loop through input
int i = 0;
if(cFlag == 1)
{
int i =0;
for( i = 0;input[i]!='\0'; i++)
{
//find upperCase letters
if(input[i] >= 'A' && input[i] <= 'Z')
{
//overwrite to lowerCase
input[i] = tolower(input[i]);
//input[i] = input[i] +32;
}//end of if statement
//ignoring punctuation
if(input[i] == ',' || input[i] == '.' || input[i] == '!' || input[i] == '?' || input[i] == '"' || input[i] == ':' || input[i] ==';' || input[i] == '-')
{
input[i] = ' ';
}
} //end of for loop
}
wordcount = 0;
for(i = 0;input[i] != '\0'; i++)
{
if(input[i] == ' ' && input[i+1] != ' ')
wordcount++;
}// end of while loop
fprintf(fo,"WordCount is: %d\n", wordcount +1);
//count occurrences
wordOccurences(input, fo);
}
if(oFlag == 1)
{fclose(fo);}
}
wordOccurrences
/*
* C Program to Find the Frequency of Every Word in a
* given String
*/
#include <stdio.h>
#include <string.h>
#include "functions.h"
wordOccurences(char *input, FILE *output)
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char str[100], p[50][100], str1[20], ptr1[50][100];
char *ptr;
// printf("Enter the string\n");
//scanf(" %[^\n]s", input);
printf("string length is %d\n", strlen(input));
for (i = 0;i<strlen(input);i++)
{
if ((input[i] == ' ')||(input[i] == ', ')||(input[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < strlen(input);j++)
{
if ((input[j] == ' ')||(input[j] == 44)||(input[j] == 46))
{
p[i][k] = '\0';
i++;
k = 0;
}
else
p[i][k++] = input[j];
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
fprintf(output,"%s -> %d times\n", ptr1[i], c);
c = 0;
}
}
wordOccurencesForFile(FILE *fp, int cFlag, FILE *output)
{
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *str = (char*)malloc(fsize + 1);
fread(str, fsize, 1, fp);
fclose(fp);
str[fsize] = 0;
int count = 0, c = 0, i, j = 0, k, space = 0;
char p[1000][512], str1[512], ptr1[1000][512];
char *ptr;
if ( fp )
{
for (i = 0;i<strlen(str);i++)
{
if (cFlag == 1)
{
//ignoring punctuation
if(str[i] == ',' || str[i] == '.' || str[i] == '!'
|| str[i] == '?' || str[i] == '"' || str[i] == ':'
|| str[i] ==';' || str[i] == '-')
{
str[i] = ' ';
}
}
if ((str[i] == ' ')||(str[i] == ',')||(str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
{
p[i][k] = '\0';
i++;
k = 0;
}
else
{
if (cFlag == 1)
{
//find upperCase letters
if(str[j] >= 'A' && str[j] <= 'Z')
{
//overwrite to lowerCase
str[j] = tolower(str[j]);
}//end of if statement
}
p[i][k++] = str[j];
}
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
fprintf(output,"%s %d \n", ptr1[i], c);
c = 0;
}
}
else
{
printf("Failed to open the file\n");
}
}

by using scanf stopped working

here is my code a part of a project when i initialize CHAR_1,CHAR_2 it works but when i read them by scanf it stopped working.
int repair_beta(char beta[11][11])
{
int i = 0, j = 0;
int count = 0, n = 0;
int fail = 0;
for(i = 1; i < 11; i++)
{
for(j = 1; j < 11; j++)
{
if(beta[i][j] == stronghold)
{
count++;
}
}
}
if(count == 4)
{
return 1;
}
else if(count < 4)
{
char CHAR_1 = 'A', CHAR_2 = '5';
for(n = 0; n < 3; n++)
{
printf("\n\t\t <<choose to repair>>\n\t\t\t ");
scanf("%c", &CHAR_1);
scanf("\n%c", &CHAR_2);
for(i = 1; i < 11; i ++)
{
if(CHAR_1 == 'A' + i - 1)
{
break;
}
}
for(j = 1; j < 11; j++)
{
if(CHAR_2 == '0' + j - 1)
{
break;
}
}
if(beta[i][j] == LAND || beta[i][j] == land)
{
beta[i][j] = stronghold;
prize_beta--;
printf("\n\t\t\t <<repaired>>");
printf("\n****************************************************************************\n");
return 1;
}
else
{
fail++;
}
}
if(fail == 3)
{
printf("\n\t\t <<you failed to repair>>");
prize_beta;
printf("\n****************************************************************************\n");
}
}
}
it is my firs time i use stackoverflow so excuse me if i ask my question in bad way.

Resources