Warm Ups

length function

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

int length(string s);

int main(void)
{
 string name = GetString();
 int l = length(name);
 printf("The length of %s is %i\n", name, l);
}

int length(string s)
{
 int n = 0;
 while (s[n] != '\0')
 {
 n++;
 }
 return n;
}

switch example

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

int main (int argc, string argv[])
{
char grade = argv[1][0];

switch(grade)
{
case 'A' :
printf("Excellent!\n");
break;
case 'B' :
case 'C' :
printf("Well done\n");
break;
case 'D' :
printf("You passed\n");
break;
case 'F' :
printf("Better try again\n");
break;
default :
printf("Invalid grade\n");
}

printf("Your grade is %c\n", grade);

return 0;
}

letters.c

#include <cs50.h>

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

int main (void)
{
char letters[26];
for (int i = 0; i < 26; i++)
{
letters[i] = 'A' + i;
}
printf("%c", letters[24]);
printf("%c", letters[4]);
printf("S\n");
}

 

backwards.c

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

int main(int argc, string argv[])
{
for (int i = argc - 1; i > 0; i--)
for (int j = strlen(argv[i]) - 1; j >=0; j--)
printf("%c", argv[i][j]);

printf("\n");
}

think.c

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

int main(int argc, string argv[])
{
for (int i = 0, n = strlen(argv[0]); i < n; i++)
{
printf("%c", argv[0][i]);
}
}