Question

The code is in C programming language pls convert it into python. Thanks. Program --> #include...

The code is in C programming language pls convert it into python. Thanks.

Program -->

#include <stdio.h>

#include <stdlib.h>

void main()

{

//declare variables

FILE *fileptr;

char filename[15];

char charRead;

char filedata[200],searchString[50];

int i=0,j=0,countNoOfWord=0,count=0;

//enter the filename to be opened

printf("Enter the filename to be opened \n");

scanf("%s", filename);

/* open the file for reading */

fileptr = fopen(filename, "r");

//check file exit

if (fileptr == NULL)

{

printf("Cannot open file \n");

exit(0);

}

charRead = fgetc(fileptr);

//read the string from file

while (charRead != EOF)

{ filedata[i]=charRead;//store the file data in a string filedata variable

printf ("%c", charRead);

charRead = fgetc(fileptr);//get data from file

i++;

  

}

//count the total no of word in a file

for (j = 0;filedata[j] != '\0';j++)

{

if (filedata[j] == ' ')

countNoOfWord++;   

}

strlwr(filedata);//convert string into lower case

  

printf("\nnumber of words in given string are: %d\n", countNoOfWord );

fclose(fileptr);

//enter the word u want to count

printf("Enter word to count: \n");

scanf("%s", searchString);

//call function countOccuranceOfWord to count the word which return integer of no of words

count = countOccuranceOfWord(filedata, searchString);

printf("Total occurrences of '%s': %d \n", searchString, count);

//write into file output.txt

fileptr = fopen("output.txt", "w");

if(fileptr == NULL)

{

printf("Error!");

exit(1);

}

char sentence1[50]="Total no. of words";

char sentence2[50]="Total occurrences of ";

//writing into output.txt

fprintf(fileptr,"%s %d %s %s %d", sentence1,(countNoOfWord),sentence2,searchString,count);

fclose(fileptr);//close fileptr

  

}

//function to countOccurance of word

int countOccuranceOfWord(char * str, char * toSearch)

{

int i, j, found, count;

int stringLen, searchLen;

stringLen = strlen(str); // length of string

searchLen = strlen(toSearch); // length of word to be searched

count = 0;

for(i=0; i <= stringLen-searchLen; i++)

{

/* Match word with string */

found = 1;

for(j=0; j<searchLen; j++)

{

if(str[i + j] != toSearch[j])

{

found = 0;

break;

}

}

if(found == 1)

{

count++;

}

}

return count;

}

Homework Answers

Answer #1

filename = input("enter the file name:")
f = open(filename,'r')
paragraph = f.read()
countnowords = 0
for i in range(0,len(paragraph)):
   if(paragraph[i] == ' ' or paragraph[i] == '.' or paragraph[i] == '\n'):
       countnowords += 1

count_string = input("enter the string to count:")
no_of_occurences = paragraph.count(count_string)

f.close()
f = open("output.txt","w")
f.write("Total no of words : {} \n Total no of occurences of {} is {}".format(countnowords,count_string,no_of_occurences))
f.close()

If you have any doubts please comment and please don't dislike.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE];...
#include #include #define MAX_SIZE 1300 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1; int count2; stringLen = strlen(str); searchLen = strlen(tosearch); count1 = 0; count2 = 0; for (cursor = 0; cursor <= stringLen; ) { if (str[cursor] == tosearch[i]) { count1++; if (count1 == searchLen) {...
I'm trying to find a code to check the occurrences of the word or string I...
I'm trying to find a code to check the occurrences of the word or string I wrote my own code but it's not working can you please help and fix my code #include <stdio.h> #include <string.h> #define MAX_SIZE 100 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1;...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char...
C CODE PLZ! Need all TO DO sections finished thanks #include <stdio.h> int main(int argc, char **argv) { const int BUF_LEN = 128; char str[BUF_LEN]; int i; char c; int is_binary; int d, n; /* Get the user to enter a string */ printf("Please enter a string made of 0s and 1s, finishing the entry by pressing Enter.\n"); for (i=0; i<BUF_LEN-1; i++) { scanf("%c", &c); if (c == '\n') { break; } str[i] = c; } str[i] = '\0'; /*...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100];...
create case 4 #include <stdio.h> int main(void) { int counter; int choice; FILE *fp; char item[100]; while(1) { printf("Welcome to my shopping list\n\n"); printf("Main Menu:\n"); printf("1. Add to list\n"); printf("2. Print List\n"); printf("3. Delete List\n"); printf("4. Remove an item from the List\n"); printf("5. Exit\n\n"); scanf("%i", &choice); switch(choice) { case 1: //add to list //get the input from the user printf("Enter item: "); scanf("%s", item); //open the file fp = fopen("list.txt","a"); //write to the file fprintf(fp, "\n%s", item); //close the file...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...
would someone kindly convert the following C code to java? thanks so much # include <stdio.h>...
would someone kindly convert the following C code to java? thanks so much # include <stdio.h> int main(void) { int changeOwed; int check; char invisibleChar; int count = 0; int numQ=0, numD=0, numN=0, numP=0; do{ printf("How much change is owed (in cents)?\n"); check = scanf("%d", &changeOwed); // returns 1 if the input is a number and returns 0 otherwise //Get's rid of any extra invisible characters if the input is a char/String do{ scanf("%c",&invisibleChar); }while(invisibleChar != '\n'); }while(check == 0...
can you please do this lab? use lunix or C program its a continuation of a...
can you please do this lab? use lunix or C program its a continuation of a previous lab. the previous lab: Unix lab 4: compile and link multiple c or c++ files Please do the following tasks step by step: create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() {       char str[100];    /*Buffer...
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");...
Explain this code function by function in detail int menu() {   int choice;   do {     system("cls");     printf("1-Insurence Plan Subscription\n");     printf("2-Claim Processing\n");     printf("3-Accounts Information\n");     printf("4-Searching Functionalities\n");     printf("5-Exit\n");     scanf("%d", &choice);   } while (choice > 5 || choice < 1);   return choice; void subscribe() system("cls");   victims[userCount].id = userCount + 1;   printf("Enter age: ");   scanf("%d", &victims[userCount].age);   printf("\n\n%-25sHealth Insurence Plan\n\n", " ");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20s|%-20s|%-20s|\n", " ", "Plan 120(RM)", "Plan 150(RM)", "Plan 200(RM)");   printf("-----------------------------------------------------------------------------------------------\n");   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Monthly Premium", 120, 150, 200);   printf("|%-30s|%-20d|%-20d|%-20d|\n", "Annual Claim Limit", 120000,150000,200000);   printf("|%-30s|%-20d|%-20d|%-20d|\n",...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
Design flow chart of this code and make sure it is not handwritten it must on...
Design flow chart of this code and make sure it is not handwritten it must on some software. struct user {    int id;    int age;    bool annualClaim;    int plan;    char name[30];    char contactNum[15];    char address[50]; }; #define MAX_LENGTH 500 struct user users[100]; int userCount = 0; struct claim {    int id;    int claimedYear;    int amountClaimed;    int remaininigAmount; }; struct claim claims[100]; void loadData() {    char line[MAX_LENGTH];    const...