#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXCANDIDATES 7…
- User
#include <stdio.h> #include <stdlib.h> #include <string.h>
#define MAX_CANDIDATES 7 #define MAX_CANDIDATE_NAME_LENGTH 100
int main() { FILE *inputFile, *outputFile; char candidateNames[MAX_CANDIDATES][MAX_CANDIDATE_NAME_LENGTH]; int candidateVoteCounts[MAX_CANDIDATES] = {0}; int totalVoteCount = 0, invalidVoteCount = 0; int candidateCount = 0, voteInput; int maxVoteCount = 0, maxNameWidth = 0;
inputFile = fopen("votes.txt", "r"); if (inputFile == NULL) { perror("Error opening input file"); return EXIT_FAILURE; } while (candidateCount < MAX_CANDIDATES && fscanf(inputFile, "%99[^\n]\n", candidateNames[candidateCount]) == 1) { candidateCount++; } while (fscanf(inputFile, "%d", &voteInput) == 1) { totalVoteCount++; if (voteInput >= 1 && voteInput <= candidateCount) { candidateVoteCounts[voteInput - 1]++; if (candidateVoteCounts[voteInput - 1] > maxVoteCount) { maxVoteCount = candidateVoteCounts[voteInput - 1]; } } else { invalidVoteCount++; } } fclose(inputFile); outputFile = fopen("Results.txt", "w"); if (outputFile == NULL) { perror("Error opening output file"); return EXIT_FAILURE; } for (int i = 0; i < candidateCount; i++) { int currentNameWidth = strlen(candidateNames[i]); if (currentNameWidth > maxNameWidth) { maxNameWidth = currentNameWidth; } } fprintf(outputFile, "Vote invalide : %d\n\n", invalidVoteCount); fprintf(outputFile, "Nombre d'electeurs : %d\n", totalVoteCount); fprintf(outputFile, "Nombre de votes valides : %d\n", totalVoteCount - invalidVoteCount); fprintf(outputFile, "Nombre de votes annules : %d\n\n", invalidVoteCount); fprintf(outputFile, "Candidat%*sScore\n", maxNameWidth, ""); for (int i = 0; i < candidateCount; i++) { fprintf(outputFile, "%s%*s%d\n", candidateNames[i], maxNameWidth - (int)strlen(candidateNames[i]) + 2, "", candidateVoteCounts[i]); } fprintf(outputFile, "\nLes gagnants:\n"); for (int i = 0; i < candidateCount; i++) { if (candidateVoteCounts[i] == maxVoteCount) { fprintf(outputFile, "%s\n", candidateNames[i]); } } fclose(outputFile); return EXIT_SUCCESS;}
change the code completely but make it still function the same, change name of variables and order in french and removedefining max_candidats or longeur nom anything and no comments
- Assistant
- User
same thing but with this code #include <stdio.h> #include <stdlib.h> #include <string.h>
#define MAX_LENGTH 1000
void replaceWord(char *str, const char *oldWord, const char *newWord) { char *pos, temp[MAX_LENGTH]; int index = 0; int oldWordLen = strlen(oldWord);
while ((pos = strstr(str, oldWord)) != NULL) { strncpy(temp, str, pos - str); temp[pos - str] = '\0'; strcat(temp, newWord); strcat(temp, pos + oldWordLen); strcpy(str, temp); index = pos - str + strlen(newWord); }}
int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: %s \n", argv[0]); return 1; }
char filename[MAX_LENGTH]; strcpy(filename, argv[1]); FILE *file = fopen(filename, "r"); if (file == NULL) { perror("Error opening file"); return 1; } fseek(file, 0, SEEK_END); long file_size = ftell(file); fseek(file, 0, SEEK_SET); char *file_content = (char *)malloc(file_size + 1); if (file_content == NULL) { perror("Memory allocation error"); fclose(file); return 1; } fread(file_content, 1, file_size, file); fclose(file); file_content[file_size] = '\0'; printf("File Content Before Replacement:\n%s\n", file_content); char oldWord[MAX_LENGTH], newWord[MAX_LENGTH]; printf("Entrez une chaine a remplacer : "); scanf("%s", oldWord); printf("Entrez la nouvelle chaine : "); scanf("%s", newWord); printf("Replacing occurrences of '%s' with '%s'\n", oldWord, newWord); replaceWord(file_content, oldWord, newWord); printf("File Content After Replacement:\n%s\n", file_content); file = fopen(filename, "w"); if (file == NULL) { perror("Error opening file"); free(file_content); return 1; } fprintf(file, "%s", file_content); fclose(file); printf("Le remplacement a ete effectue avec succes.\n"); free(file_content); return 0; - Assistant
- User
how would i create executable with gg
- Assistant
It seems there's a small typo in your question. You likely meant to ask how to create an executable with
gcc, which is the GNU Compiler Collection and a very commonly used C compiler.To create an executable with
gcc, you will need to follow these steps:-
Write your C code and save it with a
.cextension, for exampleprogram.c. -
Open a terminal or command prompt.
-
Navigate to the directory where your
program.cfile is located using thecd(change directory) command. -
Compile the C program to an executable by running the command with the desired input file name and the option followed by the desired output file name. For example:
-