30.6.25

20250630 16:00 Strings in Metamorphosis

                                              

← Previous 🏠 Homepage

                                                                                                

 - Strings in Metamorphosis



- When Letters Line Up and Do a Dance 

What are Strings?



  • A string is just a bunch of characters hanging out in a char array.

  • Like int arr[5] stores numbers, char name[10] stores characters.

  • And here's the twist: strings must end with a special guest — \0 (null character).
    Think of it like a secret agent that tells the program: “Stop! End of string here!”

Example:

char name[] = {'H','E','L','L','O','\0'};

Or, the cooler shortcut:

char name[] = "HELLO"; // C adds '\0' for you!

Why care about \0?




Without it, your string is like a sentence with no full stop.

The program won’t know when to shut up.

Printing Strings



Here’s a basic way to print strings:

while (name[i] != '\0') {
   printf("%c", name[i]);
   i++;
}

Or even cooler:

char *ptr = name;
while (*ptr != '\0') {
   printf("%c", *ptr);
   ptr++;
}

Wanna be super cool? Just do:

printf("%s", name); // Easy-peasy lemon squeezy 

Input from User:



scanf("%s", name);

BUT! Only works for one-word names like Batman.
So "Bruce Wayne"? Nope.

To get full names (multi-word), use:

gets(name); // Accepts spaces too
puts(name); // Prints it with a new line

Just don’t let your string get too long or your program might freak out. 

Pointers & Strings:


char str[] = "Hello"; // normal string
char *p = "Hello";     // string pointer

Key Difference:

  • str1 = "Bye"; (error! arrays can’t be reassigned)

  • p = "Bye"; (pointers are chill)

Also:

  • You can't do str2 = str1; 

  • But q = s; works fine if they’re pointers. 

- Spellbook of 'C'


strlen() – String Length Detector

Job: Counts how many characters are in your string (not including \0)
Example:

strlen("Bamboozled"); // 10

DIY version:

int xstrlen(char *s) {
  int len = 0;
  while (*s != '\0') {
    len++;
    s++;
  }
  return len;
}

strcpy() – Copy-Paste Master

Job: Copies one string into another

strcpy(target, source);

Make sure target is big enough — it doesn’t ask before copying! 

DIY version:

void xstrcpy(char *t, const char *s) {
  while (*s != '\0') {
    *t = *s;
    t++;
    s++;
  }
  *t = '\0'; // Don’t forget to end it!
}

strcat() – The String Gluer

Job: Joins one string to the end of another

strcat(target, source);

"Hello" + "World" = "HelloWorld"
Just make sure target has enough space… or C will go full Hulk! 

You can try making your own xstrcat() for fun!

strcmp() – String Gladiator

Job: Compares two strings to see if they’re same

strcmp("Tom", "Tom");    // 0 → perfect match
strcmp("Tom", "Tim");    // not 0 → mismatch

Returns:

  • 0 if they match (yay!)

  • >0 or <0 if they don’t (depends on ASCII)

Example:

strcmp("Jerry", "Ferry");   // returns 4 ('J' - 'F')
strcmp("Jerry", "Jerryboy");// returns -32 ('\0' - ' ')

DIY version:

int xstrcmp(char *s1, char *s2) {
  while (*s1 && *s2 && *s1 == *s2) {
    s1++;
    s2++;
  }
  return *s1 - *s2;
}


No comments:

Post a Comment

rating System

Loading...

Understanding Arrays in C

The Bookshelf Analogy & Book-Author Example Arrays are one of the most essential concepts in C programming. They let you store multiple ...