Computer Programming (11) - Strings


administrivia

Web Page - http://www.cs.tau.ac.il/~efif/courses/ComputerProgramming


so what else can we do with strings?


but we need to do more


our date program


so what do we do?


parsing the string


string operations


But...


string functions


let's talk about a simple one


example

#include <stdio.h>

int main (void)
{
  char string[80];
  int length;

  printf ("Enter a string: ");
  scanf ("%s", string);
  length = strlen (string);
  printf ("You entered %d characters.\n", length);

  return 0;
}

what does this do?


strlen()


strlen()


what other functions are there?


how might we use them?


so how can we do this?


example 1

int FindMonthNum (const char *monthName)
{
  if (strcmp (monthName, "January") == 0)
    return 1;
  else if (strcmp (monthName, "February") == 0)
    return 2;
  else if (strcmp (monthName, "March") == 0)
    return 3;
  else if (strcmp (monthName, "April") == 0)
    return 4;
  else if (strcmp (monthName, "May") == 0)
    return 5;
  else if (strcmp (monthName, "June") == 0)
    return 6;
  else if (strcmp (monthName, "July") == 0)
    return 7;
etc.

example 2

int FindMonthNum (const char * monthName)
{
  int i;
  char *month[] = {
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
  };

  for (i = 0; i < numMonths; ++i)
    if (strcmp (monthName, month[i]) == 0)
      return i+1;
  return 0;
}

those two examples...


Character Input and Output


getchar


getchar example


getchar return type


EOF


EOF


terminating a program


shorter getchar example


getline

lectures prev top next
Maintained by Efi Fogel. Last modified: January 25 2003.