Debug code to get it to only count non alpha numeric values. Right now it always outputs zero. Why? please explain
/*Problem 1*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int loop_count;
char scanned_line[100];
int non_alpha_num_checker = 0;
int total;
int
main() {
printf("Enter a line: ");
fgets(scanned_line,sizeof scanned_line, stdin);
for(loop_count = 0; loop_count != '\0'; loop_count++){
if(scanned_line[loop_count] >= 'A' &&
scanned_line[loop_count]<='Z')
{
continue;
}
else if(scanned_line[loop_count] >= 'a' &&
scanned_line[loop_count]<='z')
{
continue;
}
else if(scanned_line[loop_count] >= '0' &&
scanned_line[loop_count]<='9')
{
continue;
}
else
{
non_alpha_num_checker++;
}
}
printf("Answer: %d", non_alpha_num_checker);
return 0;
}
// Just change loop condition from loop_count != '\0' to // scanned_line[loop_count] != '\0'
/*Problem 1*/ #include <stdio.h> #include <string.h> #include <stdlib.h> int loop_count; char scanned_line[100]; int non_alpha_num_checker = 0; int total; int main() { printf("Enter a line: "); fgets(scanned_line,sizeof scanned_line, stdin); for(loop_count = 0; scanned_line[loop_count] != '\0'; loop_count++){ if(scanned_line[loop_count] >= 'A' && scanned_line[loop_count]<='Z') { continue; } else if(scanned_line[loop_count] >= 'a' && scanned_line[loop_count]<='z') { continue; } else if(scanned_line[loop_count] >= '0' && scanned_line[loop_count]<='9') { continue; } else { non_alpha_num_checker++; } } printf("Answer: %d", non_alpha_num_checker); return 0; }
Get Answers For Free
Most questions answered within 1 hours.