CS50 Readability, How to print the total number of letters? - c

Hey I've started on the readability problem for cs50 and I'm stuck on step two. I'm sure it's a simple fix but I've already spent an hour or so racking my brain on it to no avail. Basically I just want to print off the total number of letters but when I print off the integer it prints off the entire sequence.
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
int main(void)
{
string s = get_string("Text:");
printf("%s\n", s);
for (int i = 0; i < strlen(s); i++)
{
printf("%i\n", i);
}
}
which gives me this
Text:hello
hello
0
1
2
3
4

This will do the job for you.
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void)
{
string s = get_string("Text:");
int length = 0;
length = strlen(s);
printf("%i\n", length);
}
However, this will take spaces into account as well. Only counting the letters, you have to build for loop to check every single character if it is alphabetic or not. There are built in functions for this. Please do research.

Related

Why does printing multiple zero width spaces print less than expected in C?

The program below is supposed to print A, 100 zero width spaces or '0x200B' and then print B, but when I execute it, it prints 10 zero width spaces instead
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main() {
setlocale(LC_CTYPE, "");
wchar_t space = 0x200B;
wprintf(L"A");
for (int i = 0; i < 100; i++) {
wprintf(L"%lc", space);
}
wprintf(L"B");
}
I count these by copy pasting the output from the bash terminal into a online char counter so there. Why does this happen if it happens to anyone else at all?

My code does not print numbers of letters in a given text. Why?

I am trying to create a function as below that would count the number of letters in an inputted text and spit out an integer value. My code below compiles but it won't print out the result. Am I missing something?
// Libraries
#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
int count_letter(string text)
{
int lettercount;
int number_of_letters;
number_of_letters = strlen(text);
for(lettercount = 0; lettercount < number_of_letters;)
if (isalpha(number_of_letters))
lettercount++;
return lettercount;
}
int main(void)
{
string text = get_string("text: ");
{
printf("%i letter(s)", count_letter(text));
printf("\n");
}
}
Since number_of_letters is an int, what do you think isalpha(number_of_letters) evaluates to? Not to mention there are no braces {} surrounding the for loop or the if in the function which makes the code hard to read, and could in fact cause results you don't expect.
To be clear, this test if (isalpha(number_of_letters)) isn't correct. Since program wants to count the number of characters in text that are alpha, the test would be something like if (isalpha(text[lettercount])).

Anagram - Minimum Number of deletion - failing certain test cases

This is a program that to solve the following question "Given two strings, and , that may or may not be of the same length, determine the minimum number of character deletions required to make and anagrams. Any characters can be deleted from either of the strings". In the end, both strings should have the same letters and same frequency of each letter. For e.g., String A = ccda String B = dcac
My logic is to replace the letter that are same in both strings with a dummy string say "0". So when I count the number of letter in each string that is not equal to "0", it gives me the number of deletion.
But I don't know why this fails for certain cases.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int count =0;
const char dummy= '0';
int i =0, j=0;
char* a = (char *)malloc(512000 * sizeof(char));
scanf("%s",a);
char* b = (char *)malloc(512000 * sizeof(char));
scanf("%s",b);
for (i=0;a[i]!= '\0' ;i++){
for(j=0; b[j]!= '\0';j++){
if (a[i]==b[j]){
a[i]= dummy;
b[j]= dummy;
}
}
}
for (i=0;a[i]!= '\0' ;i++){
if(a[i]!= dummy){
count = count+1;
}
}
for (i=0;a[i]!= '\0' ;i++){
if(b[i]!= dummy){
count = count+1;
}
}
printf("%d",count);
return 0;
}
One of the test case it failed was
String A : fcrxzwscanmligyxyvym
String B : jxwtrhvujlmrpdoqbisbwhmgpmeoke
Result given : 22
Expected result : 30
Can anyone please point me the error here. Please, thanks in advance!
You've an error in your code - invalid condition in second loop.
for (i=0;a[i]!= '\0' ;i++){
if(a[i]!= dummy){
count = count+1;
}
}
for (i=0;a[i]!= '\0' ;i++){
^^^^
if(b[i]!= dummy){
count = count+1;
}
}
In marked point should be b[i] rather than a[i].
Minor nitpicking: since you're learning to code, try to get few useful habbits as you go. Code should be pretty (not very pretty, mind you, but ascethic) - it helps to ease reading for both you and everyone else. Consequence matters. If you do spaces around operators, do it everywhere. Layout matters. All of that would help you notice your (common to pros as well, they just find it sooner) mistake.
Also there's a better algorithm in terms of running performance. :)

How to print 0 to 9 digits without using any digit in our code?

Recently, I found a problem on hackerearth that is how to print 726 without any digits. This is the link to question :https://www.hackerearth.com/challenge/college/r1/golf/726/
I have solved this with this:
#include <stdio.h>
main()
{
printf("%d",('\e'*'\e')-('\r'-'\n'));
}
but since I found this question interesting, I would love to print every single digit without use of any digit. So, I want to print 0 to 9 all in a new line without using any digit with the help of unicodes. How could I do that?
#include <stdio.h>
int main(void){
for(int n = !&n; snprintf(NULL, !&n, "%d", n)==!!&n; printf("%d\n", n++));
}
#include <stdio.h>
#define ZERO ('a' - 'a')
#define ONE ((int)(sizeof (char)))
#define TWO (ONE + ONE)
#define THREE (TWO + ONE)
#define SIX (TWO * THREE)
#define SEVEN (SIX + ONE)
int main(void) {
printf("%d%d%d\n", SEVEN, TWO, SIX);
/* Assume C99 and implicit return 0; */
}
There's countless of ways to write obscure, nonsense C code. One example:
#include <stdio.h>
int main (void)
{
for(size_t i=!"A string."; i<sizeof("A string."); i++)
printf("%zu ", i);
}
Or, more creatively, in pure standard C:
??= include"stdio.h"
#define wtf(x,...) x%:%: nt ma ##x %:%:n(vo ##x %:%:d) ??<for(x %:%:nt x =(+ __LINE__??!??!\
+__LINE__) +- !!"WTF"; x<=__LINE__; +i++)??<\
pr ##x %:%:nt %:%:f((char[])<%'%','d',' ',!"WTF"??>,-+-x);%>??>
wtf(i)
#include <stdio.h>
void main()
{
char ch='/';
while(ch!=':')
{
if(isdigit(ch))
{
printf("%c\n ",ch);
}
ch++;
}
}
You can check the values of ASCII code an use the char with %d placeholder for extract the number.
E.g. :
#include <stdio.h>
int main(int argc, char **argv)
{
char a='H';
printf("%d",a);
return 0;
}

array of string output is a clover symbol

No matter what the 5th input the output was clover symbol, program purpose was to align right the inputs:
EDIT
im not using scanf(%[\^n],a[i]), the output was horrible, using gets instead
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char a[6][50];
int i;
for (i=1;i<=5;i++)
{
printf("insert name%d :\n",i);
gets(a[i]);
}
for (i=1;i<=5;i++)
{
printf("%d%25s\n",i,a[i]);
}
return 0;
}
Example output:
1 far cry
2 iron man
3 new super mario
4 program
5 "clover"
Using your code, I got strange chars on output as you mentioned.
So, I made some fixes on your code and now I think it works properly.
#include <unistd.h>
int main(void)
{
char a[6][50];
int i, r;
for (i = 1; i <= 5; i++) {
printf("insert name%d :\n", i);
r = read(STDIN_FILENO, a[i], 49);
a[i][r] = '\0';
}
for (i = 1; i <= 5; i++) {
printf("%d%25s\n", i, a[i]);
}
return 0;
}
Avoid to use deprecated functions in your code.
You need to ensure that after reading from stdin, your buffer is going to get the null terminator right after the last position written in buffer.

Resources