There is a pointer array in the following code (char * newName) which is giving me some trouble. I believe the issue is somewhere between the backslashes i used to create barriers below. I have attached a picture below. All of the output is working correctly, however, there are 3 outputs of strings right under the word "santiago" that all begin with a strange unicode character and I am stumped as to why that is happening. The letters that come after the strange character (tbuq, bwi, etc) are all correct as well, so I just need to get rid of that character. I would greatly appreciate some guidance here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int compare(const void* a, const void* b)
{
return strcmp(*(const char**)a, *(const char**)b);
}
void sort(char* names[], int n)
{
qsort(names, n, sizeof(char*), compare);
}
int main()
{
int i, j, k, n;
char alphabet[26];
for(i = 0; i < 26; i++)
{
alphabet[i] = i + 97;
}
char newAlphabet[26];
printf("input the new alphabet\n");
for(i = 0; i < 26; i++)
{
scanf(" %c", &newAlphabet[i]);
}
printf("How many names will you input?");
scanf(" %d", &n);
char * names[n];
printf("Enter the names: \n"); //erase before submitting
//the user will now enter the names one by one
for (i = 0; i < n; i++)
{
names[i] = (char *) malloc(100 * sizeof(char)); //allocating memory of 100 for each name pointer
if (names[i] == 0)
{
printf("Error.");
exit(1);
}
scanf("%s", names[i]);
}
for(i = 0; i < n; i++)
{
printf("%s\n", names[i]);
}
int nameConvert[n][100];
//convert the strings of names to their corresponding indexes based on the a = 0, b = 1, etc
for(k = 0; k < n; k++)
{
for(i = 0; i < (strlen(names[k])); i++)
{
for(j = 0; j < 26; j++)
{
if((*(names[k] + i)) == alphabet[j])
{
nameConvert[k][i] = j;
break;
}
}
}
}
////////////////////////////////////////////////////////////////////////////
char * newName[n];
for(i = 0; i < n; i++)
{
newName[i] = '\0';
}
for (k = 0; k < n; k++)
{
newName[k] = (char* ) malloc(100 * sizeof(char));
for(i = 0; i < (strlen(names[k])); i++)
{
char tempstr[2];
tempstr[0] = newAlphabet[nameConvert[k][i]];
tempstr[1] = '\0';
strcat(newName[k], tempstr);
}
printf("%s\n", newName[k]);
}
/////////////////////////////////////////////////////////////////////////////
for(i=0; i < n; i++)
{
for(j = 0; j < strlen(names[i]); j++)
{
printf("%d ", nameConvert[i][j]);
}
printf("\n");
}
sort(newName, n);
for(i = 0; i < n; i++)
{
printf("%s\n", names[i]);
}
return 0;
}