Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Esercitazione di Laboratorio: Tecniche e Linguaggi di Programmazione
* III Facoltà di Ingegneria: Ingegneria dell'Informazione
* Politecnico di Torino, a.a. 2009/2010
*
* Copyright (C) 2009 Andrea Turso
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl.txt
* @author Andrea Turso <trashofmasters@gmail.com>
*/
#include <assert.h>
#include <stdio.h>
#define LINE_MAX_LENGTH 101
int stringLength(char*);
char *stringReverse(char*);
char *stringTrim(char*);
int endOfString(char);
int main(int argc, char *argv[])
{
// Files
FILE *inputFile, *outputFile;
if (NULL == (inputFile = fopen(argv[1], "r"))) {
printf("Errore apertura sorgente dati\n");
return 1;
}
if (NULL == (outputFile = fopen(argv[2], "w"))) {
printf("Errore aprtura destinazione dati\n");
return 1;
}
char line[LINE_MAX_LENGTH];
while (fgets(line, LINE_MAX_LENGTH, inputFile)) {
if ('\n' == *line || ' ' == *line) {
continue;
}
fprintf(outputFile, "%s", stringReverse(line));
if (!feof(inputFile)) {
fprintf(outputFile, "\n", "");
}
}
return 0;
}
int stringLength(char *string)
{
int length;
for (length = 0; !endOfString(string[length]); length++);
return --length;
}
char *stringReverse(char *s)
{
int j;
int i;
int length = stringLength(s);
char temp;
for (i = 0, j = length + 1; i < --j; i++) {
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
s[length + 1] = '\0';
return s;
}
int endOfString(char c)
{
return (0x00 == c) || (0x0a == c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.