I have a .txt file and I want to sort it using C programming. I have this code for sorting a .txt file:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 100 // Length of each line in input file.
int main(void)
{
char *strFileName = "/home/milad/Desktop/ddd.txt";
char *strFileSummary = "/home/milad/Desktop/ddd2.txt";
char strTempData[MAX_LEN];
char **strData = NULL; // String List
int i, j;
int noOfLines = 0;
FILE * ptrFileLog = NULL;
FILE * ptrSummary = NULL;
if ( (ptrFileLog = fopen(strFileName, "r")) == NULL ) {
fprintf(stderr,"Error: Could not open %s\n",strFileName);
return 1;
}
if ( (ptrSummary = fopen(strFileSummary, "a")) == NULL ) {
fprintf(stderr,"Error: Could not open %s\n",strFileSummary);
return 1;
}
// Read and store in a string list.
while(fgets(strTempData, MAX_LEN, ptrFileLog) != NULL) {
// Remove the trailing newline character
if(strchr(strTempData,'\n'))
strTempData[strlen(strTempData)-1] = '\0';
strData = (char**)realloc(strData, sizeof(char**)*(noOfLines+1));
strData[noOfLines] = (char*)calloc(MAX_LEN,sizeof(char));
strcpy(strData[noOfLines], strTempData);
noOfLines++;
}
// Sort the array.
for(i= 0; i < (noOfLines - 1); ++i) {
for(j = 0; j < ( noOfLines - i - 1); ++j) {
if(strcmp(strData[j], strData[j+1]) > 0) {
strcpy(strTempData, strData[j]);
strcpy(strData[j], strData[j+1]);
strcpy(strData[j+1], strTempData);
}
}
}
// Write it to outfile. file.
for(i = 0; i < noOfLines; i++)
fprintf(ptrSummary,"%s\n",strData[i]);
// free each string
for(i = 0; i < noOfLines; i++)
free(strData[i]);
// free string list.
free(strData);
fclose(ptrFileLog);
fclose(ptrSummary);
return 0;
}
This code is case sensitive and it sorts the upper-case letters first, then the lower case letters and That is not what I want. I want it to sort letters alphabetically and without any sensitivity to the case of the letter. I know about ASCII codes and why I have this problem, but I can't find a way to fix it.
How can I change the code to make it case insensitive?