In my if statement I am trying to say, "if there is a space in strlen subtract that space then print strlen." My goal is to count only the letters that appear in the string and not the spaces. If there is something I can do to fix a nudge in the right direction would be appreciated. Im currently down to one error.

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

int main (void)

   int x = ' ';
// prompt user for text
   string t = get_string("Enter Text:\n");
   if(strlen(t) == x)
       strlen(t) - x;
   else printf("letters: %lu\n" , strlen(t));
// for every letter count 1 and print total


Disclaimer: before I had x as an integer I was using ' ' in it's place to represent a space(' '). I thought maybe if I gave space(' ') a value such as x. the program would be able to understand x is a space. take x out. this didn't work for me though.

error message reads: clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    readability.c  -lcrypt -lcs50 -lm -o readability
readability.c:13:18: error: expression result unused [-Werror,-Wunused-value]
       strlen(t) - x;
       ~~~~~~~~~ ^ ~

1 Answer 1

string input = get_string("Text:"); //getting input from the user
int len = strlen(input); //determining the length of the string
int space = 0; //declaring a variable to count whitespaces
for (int i = 0; i < len; i++) //declaring a for loop to check each element of the string
        if (input[i] == ' ')
len = len - space; //removing all whitespaces and keeping only letters and special characters

This code will be able to solve your problem of detecting whitespaces and eliminating them.

Problems in your code

  • Declaring a whitespace using int is not a good practice.
  • When you are writing (strlen(t) == x ), this means you are comparing just the length of the string to a variable. You will not be able to access each of the element of the string separately.
  • I tried your solution and I think I know what I was doing wrong. My program didn't understand the number of spaces. In your for loop you gave spaces a numerical value that the computer could understand. Im just learning about Arrays and don't fully understand them. I was confused as to why input[i] was an "i". I think maybe because i is unimportant and just a place holder for any letter? The computer understood that "i" was representative of the whole string we get from the user and now has access to each individual char, space, and punctuation in the text, making it able to identify spaces?
    – kenn92
    Nov 7, 2020 at 16:05
  • You can understand input[i] array as flash cards, and each flash card contains a value (letter, number, whitespace or special characters). The if statement that I used can be compared with a reference flash card having the value equal to whitespace. When you turn each flash card in order from input[0] to input[max] you will compare that with the reference flash card. While turning the flash cards if the face value matches the counter will increase. Nov 7, 2020 at 16:47
  • Thanks for your help. I went back and thought about it some more. i = 0. 0 is the first place in string. is 0 a space? no. Nothing happens. 0 becomes 1 because of i++. it does this over and over again until it gets through the entirety of the strings and i is no longer less than string length. I was thinking about it the wrong way the first time. [i] represents where you are at in the string of (t)
    – kenn92
    Nov 7, 2020 at 17:52
  • Welcome, it would be nice if you would upvote the answer for future references. Nov 8, 2020 at 12:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .