How do I edit my code so that I may output the frequency of TWO specific characters input by a user? (C Language)

user9393548

I have tried for hours to edit this code to work the way I want it to. I am meant to code a program to track the frequency of how often an underscore '_' and an exclamation point '!' appear in a sentence input by a user: _Hi_there!!!

Specifications that must be used and/or not deleted -This function prototype must remain unmodified: -The getchar() method must be used to hold the value of the sentence input by the user.

Now I have this code after hours of struggling to get a proper output:

#include <stdio.h>

int num_count;
int num_exclamation_count;
char ch;
char s[1000]; 
void count(int* num_, int* num_exclamation)
{       
    int i;
    for(i = 0; s[i] != '\0'; i++)
    {
        if((s[i] == getchar()) && (s[i] == '_'))
        {
            ++num_count;
            *num_ = num_count;  
        }
        else if((s[i] == getchar()) && (s[i] == '!'))
        {
            ++num_exclamation_count;
            *num_exclamation = num_exclamation_count;
        }
        else
        {
            continue;
        }
    }

}

int main (void)
{
    int num_user, num_exclamation_user;
    printf("Enter a sentence: ");
    do
    {
        ch = getchar();
    }while(ch != '\n');
    count(&num_user, &num_exclamation_user);

    printf("There are %d underscores and %d exclamation points in the 
    sentence.\n", num_user, num_exclamation_user);

    return 0;
}

The output I get is as follows:

There are 0 underscores and 0 exclamation points in the sentence.

I tried every variation of while or if or do-while statements I could conjure in my mind or find available online and I get nowhere but further away. If someone could thoroughly explain how I arrange which conditional statement/loop with the getchar() method and necessary variables, that would be awesome. Open to criticism as well if I blatantly screwed something up or passed over an obvious issue, do not be scared to hurt my feelings. I only want to learn and will be my only mindset as I am assisted with this problem.

Thank you.

chux - Reinstate Monica

OP's code has these errors:

  1. Not initializing num_user, num_exclamation_user in main(). Set them to 0

  2. Unneeded code of do { ch = getchar(); while (ch != '\n'); in main(). Delete

  3. Looking for a null character from input with for(i = 0; s[i] != '\0'; i++) prevented loop iteration as s[0] is initialized to 01. User input is lines, not a string. Look for a '\n'. Also assign s[i] = getchar(), not compare s[i] == getchar() @wildplasser

    //for (i = 0; s[i] != '\0'; i++) {
    for (i = 0; i < 1000 && (s[i] = getchar()) != '\n'; i++) {
    
  4. Code calls getchar() more than once per loop.

    // if ((s[i] == getchar()) && (s[i] == '_')) {
    if (s[i] == '_') {
      ...
    // } else if ((s[i] == getchar()) && (s[i] == '!')) {
    } else if (s[i] == '!') {
    

Although there are other learner issues (e.g. better to use int ch for input than a char s[i]), fixing the above will solve OP's problem.

1 Global integer types like the array s[1000] elements are initialized to 0.

Short count() below. Think it out before mousing over.

void count(int* num_, int* num_exclamation) {
int ch; while ((ch = getchar()) != '\n' && ch != EOF) {
if (ch == '') {
(*num
)++;
} else if (ch == '!') {
(*num_)++;
}
}
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I edit my code so that I can account for output that goes against my sed command

How do i make this pattern from user input using a for loop (i want my code to be able to produce the output on the bottom)

I am not getting any output in my node.js code how do I edit it?

How do i output a user input string

How do I output my audio input?

How do I limit my user input

Language: C How do I get my code to only print a message when the inputted text from the user meets a certain criteria?

How can I edit this code for user input and to end at 0?

How do i change my code so that if theres a second input it will add onto my histogram?

How do I output answer of my user input knowledge base in Prolog

How do I edit this code so that [‘7005000’] is printed twice

How do I have `w` command output 10 characters for USER?

How do I split up a user input so that it takes up two index places in an array? (Python)

How do I edit a URL's location and use it for my code

How do I display all the characters between two specific strings?

How can I edit the output of my dataframe?

How do I fix my Java code so that it ends when a user types "quit"?

How do I fix my code so it allows me to grab the user's id who entered the command?

How do i code so that my button after clicking will direct user to the footer in the same html?

How do I use recursion to loop my code (when user enters invalid input, it prompts them again)?

How do I make syntax highlighting for a specific language with code mirror

How do I use the user input to write the output using Tkinter?

How do I create an output dataframe that uses user input in the name?

How do I append my output at a specific position in python?

How can I make my code respond to user input

How can I get my code to restart based on user input?

How can I run my code during waiting user input

How do I output the difference between two specific revisions in Subversion?

How do i pick out specific strings from a user input?