Question

Debug code to get it to only count non alpha numeric values. Right now it always...

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;
}

Homework Answers

Answer #1
// 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;
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Compile and run the following code and presuming your executable is in a.out, run this command...
Compile and run the following code and presuming your executable is in a.out, run this command from the shell prompt: ./a.out ; echo "Parent Terminated Normally with a return value of $?" Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Hint: This has everything to do with how processes “communicate” their exit...
I'm trying to find a code to check the occurrences of the word or string I...
I'm trying to find a code to check the occurrences of the word or string I wrote my own code but it's not working can you please help and fix my code #include <stdio.h> #include <string.h> #define MAX_SIZE 100 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1;...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str...
use c language only to solve this porblem -------------------------- Have the function StringChallenge(str) take the str string parameter being passed and return the number of words the string contains (e.g. "Never eat shredded wheat or cake" would return 6). Words will be separated by single spaces. Examples Input: "Hello World" Output: 2 Input: "one 22 three" Output: 3 you have the following code what edits can you do #include <stdio.h> #include <string.h> void StringChallenge(char str[]) {      // code goes...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE #include <string.h> #include <stdlib.h> #include...
SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE #include <string.h> #include <stdlib.h> #include <stdio.h> #define MAX_STR_LEN 100 struct Node { char *data; struct Node *left; struct Node *right; }; struct Node *new_node(char *d) { struct Node *r = malloc(sizeof(struct Node)); r->data = malloc(sizeof(char) * MAX_STR_LEN); strncpy(r->data, d, MAX_STR_LEN); free(d); r->left = NULL; r->right = NULL; return r; } void freeSubTree(struct Node *r) { free(r->data); if (r->left != NULL) freeSubTree(r->left); if (r->right != NULL) freeSubTree(r->right); free(r); } void...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
The code is in C programming language pls convert it into python. Thanks. Program --> #include...
The code is in C programming language pls convert it into python. Thanks. Program --> #include <stdio.h> #include <stdlib.h> void main() { //declare variables FILE *fileptr; char filename[15]; char charRead; char filedata[200],searchString[50]; int i=0,j=0,countNoOfWord=0,count=0; //enter the filename to be opened printf("Enter the filename to be opened \n"); scanf("%s", filename); /* open the file for reading */ fileptr = fopen(filename, "r"); //check file exit if (fileptr == NULL) { printf("Cannot open file \n"); exit(0); } charRead = fgetc(fileptr); //read the string...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what...
For each part labeled P(n), there is a warning/error/problem that goes with it. Write down what the issue was in the `Error:` section of each problem. And fix the code to make it work. // P0 #include <stdio.h> #include <stdlib.h> /* Error: */ void fib(int* A, int n); int main(int argc, char *argv[]) { int buf[10]; unsigned int i; char *str; char *printThisOne; char *word; int *integers; int foo; int *bar; char *someText; // P1 for (i = 0; i...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT