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'; /*...
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",...
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...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the...
LANGUAGE C The codes below reads another .c or .txt file. Then, it will read the contents of every line and store the contents into a string array. Example: .c file that is to be read: #include <stdio.h> int main(){    printf("Hello World");    return ; } _______________(END OF THE FILE)_______________ Therefore, the code will read the contents above and store the contents in every line into an array, such that.... array[0] = "#include <stdio.h>\n" array[1] = "\n" array[2] ="int...
please use linux/unix command terminal to complete, step 1 1-create a new directory named by inlab4...
please use linux/unix command terminal to complete, step 1 1-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 to hold reversed string */ reverse("cat", str); /*Reverse the string "cat" */ printf("reverse(\"cat\")=%s\n", str); /*Display result */ reverse("noon", str); /*Reverse the string "noon" */ printf("reverse(\"noon\")=%s\n", str); /*Display result */ } reverse(char *before,...