I've been trying to write a program which would replace all letters in a string with their positions in alphabet. I came up with a code, which appears to be working during debugging but for some reason it won't print the result string.
During debugging string variable in main appears to be correct but it still won't print.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
char *alphabet_position(char *text) {
char string[100] = {};
char temp_c[5] = {};
int i, temp_i;
for (i=0; i<strlen(text); i++){
if (text[i] >= 65 && text[i] <= 90){
temp_i = text[i] - 'A' + 1;
snprintf(temp_c, 4, "%d ", temp_i);
strcat(string, temp_c);
}
if (text[i] >= 97 && text[i] <= 122){
temp_i = text[i] - 'a' + 1 ;
snprintf(temp_c, 4, "%d ", temp_i);
strcat(string, temp_c);
}
}
return string;
}
int main()
{
char *string = "The sunset sets at twelve o' clock.";
char *temp = alphabet_position(string);
printf("%s" ,temp);
return 0;
}
Also, I can't really understand why using char *string instead of char string[100] causes segmentation fault during strcat call.