I'm trying to get my program to store only letters in two arrays. So far I have:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
main () {
printf("Enter something: ");
char temp[100];
fgets(temp, 100, stdin);
int c;
int letters = 0;
for (c = 0; c <= strlen(temp); c++) {
if (isalpha(temp[c])) {
letters++;
}
}
int *ptr;
ptr = &letters;
char str[*ptr];
char rev[*ptr];
printf("Letters amount: %d\n", *ptr);
int i;
int k = 0;
for (i = 0; i < strlen(temp); i++) {
if (isalpha(temp[i])) {
str[k] = temp[i];
k++;
}
}
k = 0;
for(i = strlen(str)-1; i >= 0; i--){
rev[k] = str[i];
k++;
}
printf("You entered: %s\n", str);
printf("The reverse: %s\n", rev);
}
If I put "Test":
You entered: Test
The reverse: tseT
If I put "Testing One Two Three":
You entered: TestingOneTwoThree
The reverse: eerhTowTenOgnitseT@
If I put "Testinggg":
You entered: Testingggggaj<@
The reverse: @<jagggggnitseT
I have to get str[] and rev[] to store only letters because I will have to compare them later.
I understand that it has something to do with the null-byte terminator in arrays, so how can I adjust my code so that it stores the letters correctly?