Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * PROVA DI FONDAMENTI DI PROGRAMMAZIONE 22.01.2018
- *
- * In un file sono memorizzati i CARATTERI che compongono una stringa.
- * In ogni linea c'è il carattere effettivo, seguito da una sequenza di CARATTERI che
- * rappresentano le posizioni in cui appare quel carattere. Ad esempio:
- * b 2 4 5
- * significa che il carattere 'b' apparirà nella stringa nelle posizioni di indice
- * '2', '4' e '5'.
- *
- * Scrivere un programma C che letta da file una sequenza di caratteri, ricostruisca la
- stringa contenente tali caratteri (per semplicità si supponga di avere stringhe al più di
- 10 caratteri).
- * Il programma deve stampare a video il numero di caratteri che compongono la stringa
- * e salvare in un file la stringa ottenuta, dopo aver sostituito con "$" un carattere
- * digitato da tastiera.
- *
- * Un esempio di file di input e' il seguente:
- b 0
- u 1
- n 3 8
- g 4
- i 5
- r 7
- o 2 6 9
- Un esempio di esecuzione e':
- Digitare il nome del file contenente la stringa da ricostruire: parola.txt
- stringa letta: buongiorno
- I caratteri che compongono la stringa sono: 10
- Inserire nome file su cui scrivere la stringa: outparola.txt
- carattere da modificare: n
- Nel file outparola.txt apparirà il seguente testo:
- buo$gior$o
- *
- * NOTA: IL PROGRAMMA DEVE ESSERE STRUTTURATO MEDIANTE FUNZIONI E PROCEDURE.
- * NON E' POSSIBILE IMPORTARE NUOVE LIBRERIE.
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #define DIM 30
- int get_string(FILE *, char[]);
- void replace_char_string(char[], char, int);
- void save_string(FILE *, char[]);
- int main(){
- FILE * fp;
- char filename[DIM], string[DIM], ch;
- int size;
- puts("Insert input filename:");
- scanf("%s", filename);
- printf("Opening %s...\n", filename);
- if((fp=fopen(filename, "r"))==NULL){
- puts("Fatal error: unable to open file!");
- exit(1);
- }
- else
- puts("File opened successfully!");
- size=get_string(fp, string);
- printf("%d characters in the string\n", size);
- fclose(fp);
- puts("Insert output filename:");
- scanf("%s", filename);
- printf("Opening %s...", filename);
- if((fp=fopen(filename, "w"))==NULL){
- puts("Fatal error: unable to open file!");
- exit(1);
- }
- else
- puts("File opened successfully!");
- puts("Insert char to be replaced:");
- while ((ch = getchar()) != '\n' && ch != EOF) { } /* in order to clear stdin buffer */
- scanf("%c", &ch);
- printf("Replacing every %c in the string %s...\n", ch, string);
- replace_char_string(string, ch, size);
- printf("Saving the string \"%s\" in \"%s\"", string, filename);
- save_string(fp, string);
- fclose(fp);
- return EXIT_SUCCESS;
- }
- int get_string(FILE * fp, char str[]){
- char ch, temp;
- int pos, len=0;
- do{
- fscanf(fp, "%c", &ch);
- while((fscanf(fp, "%d", &pos))==1){
- str[pos]=ch;
- if(pos>len)
- len=pos;
- fgetc(fp);
- }
- puts("NL");
- }while(!(feof(fp)));
- str[len+1]='\0';
- return len;
- }
- void replace_char_string(char str[], char ch, int size){
- while( size --> 0 )
- if(str[size]==ch)
- str[size]='$';
- }
- void save_string(FILE * fp, char str[]){
- fputs(str, fp);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement