Home > OS >  Counting Characters and Strings n C
Counting Characters and Strings n C

Time:06-22

This is one of the assignments for my class and this is the objective of the assignment:

Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly 1. You may assume that the string does not contain spaces and will always contain less than 50 characters.

This is the code I have so far and I am new to C programming so I don't know how to declare Strings correctly just yet. So far I learned there are no strings in C like there is in Java and you have to do them as a character array:

#include <stdio.h>
#include <string.h>

int main(void) {

    char userChar;
    char userString[50];
    int count = 0;
      
    for (int i = 0; i < userChar; i  ) {
        if (userString[i] == userChar)
            count  ;
    }
      
    printf("%d", count);
    if (count != 1)
        printf("'s");
      
    return 0;
}

For example, if I wanted to input n Monday and output 1 n

What would I need to change in my code to go from n Monday to 1 n

This is the only output I am getting, and it only has outputted one thing correctly:

0's

CodePudding user response:

First, I hope this is not considered cheating :-)

Second, you need to define userChar and userString as arguments for main, and pass them in at run time. They are assigned nothing, so that is why you get

0's

Third, your for condition is wrong. You need this so it only iterates through the length of the string:

for (int i = 0; i < strlen(userString); i )

Finally, You are not printing the value of userChar prior to the return

CodePudding user response:

The expected output is not 0's, it should include the counted character: for example if the character is n and the string Monday, the output should be

1 n

and if the string is Many Mondays, the output would be

2 n's

Here is a modified version:

#include <stdio.h>

int main() {

    char userChar;
    char userString[50];
    int i, count;

    printf("Enter character: ");
    scanf(" %c", &userChar);
    printf("Enter string (single word): ");
    // read a word with at most 49 characters
    scanf(" Is", userString);
    
    count = 0;
    for (i = 0; userString[i] != '\0'; i  ) {
        if (userString[i] == userChar)
            count  ;
    }
      
    printf("%d %c", count, userChar);
    if (count != 1)
        printf("'s");
    printf("\n");
      
    return 0;
}

CodePudding user response:

At first you need to input a string and a character. To count the number of occurrences of the character in the string you can use standard string function strchr.

The program can look something like the following

#include <stdio.h>
#include <string.h>


int main(void) 
{
    char userChar = ' ';
    char userString[50] = "";
  
    printf( "Enter a string without embedded spaces\nof the length less than %d: ", 50 );
    scanf( "Is", userString );

    printf( "Enter a character to search in the string: " );
    scanf( " %c", &userChar );

    size_t n = 0;

    for ( const char *p = userString; ( p = strchr( p, userChar ) ) != NULL;   p )
    {
          n;
    }
     
    printf( "%zu%s %c\n", n, n < 2 ? "" : "'s", userChar ); 
}
  • Related