String Literals


Mutable Strings


String Equality


#include <stdio.h>

int stringEqual(const char *str1, const char *str2)
{
  // const makes sure that the string is not modified
  const char *p1 = str1; // p1 points to the first character of str1
  const char *p2 = str2; // p2 points to the first character of str2

  while (*p1 == *p2)
  {
    if (*p1 == '\\0')
    {
      return 1;
    }
    p1++;
    p2++;
  }
  return 0;
}

int main()
{
  char *str1 = "Hello";
  char *str2 = "Jello";

  if (stringEqual(str1, str2))
  {
    printf("The strings are equal\\n");
  }
  else
  {
    printf("The strings are not equal\\n");
  }

  return 0;
}

String Copying


Converting Strings to ints


Project: Reverse function

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

void reverse(char * str) {
	if (str == NULL) {
		return;
	}
	int len = strlen(str);
	int i, j;
	char temp;

	for (i = 0, j = len - 1; i < j; i++, j--) {
		temp = str[i];
		str[i] = str[j];
		str[j] = temp;
	}
}

int main(void) {
  char str0[] = "";
  char str1[] = "123";
  char str2[] = "abcd";
  char str3[] = "Captain's log, Stardate 42523.7";
  char str4[] = "Hello, my name is Inigo Montoya.";
  char str5[] = "You can be my wingman anyday!";
  char str6[] = "Executor Selendis! Unleash the full power of your forces! There may be no tomorrow!";
  char * array[] = {str0, str1, str2, str3, str4, str5, str6};
  for (int i = 0; i < 7; i++) {
    reverse(array[i]);
    printf("%s\\n", array[i]);
  }
  return EXIT_SUCCESS;
}