<< Prev Next >>
Variables: working with character strings
As I said previously, working with character strings is a bit different than numeric data types. For C, the char* data type used to represent a character string data type is a mere indicator on where the string is.
Thus it doesn't reserve memory space for the string, contrary to the numeric or char data types. You have no space to store a string if you just declare it this way:
Code: Select all
char* string;
On the other hand, declaring it like this:
Code: Select all
char* string = "Hello";
There's also another way to make the compiler create space without having to define any default string, like this:
Code: Select all
char string[80];Since the char* and char[] data types are mere indicators, we cannot copy a string like this:
Code: Select all
char* string0 = "Hello";
char string1[20];
string1 = string0;Code: Select all
#include <stdio.h>
// Functions to work with strings
#include <string.h>
int main(void)
{
// Declaring and assigning value to two character strings
char* string0 = "Hello world!";
char* string1 = "This is just\n\tanother sad string";
// Declaring an empty string with a capacity for 100 characters
char string2[100];
// Setting the buffer to all zeroes (ASCII code 0, NUL character)
memset(string2, 0, 100);
printf("string0 = %s\n", string0);
printf("string1 = %s\n", string1);
// Getting string0 length with strlen()
unsigned int len_string0 = strlen(string0);
// Showing both strings length
// Notice how we can also directly use an expression for printf arguments
printf("string0 length = %u\n", len_string0);
printf("string1 length = %u\n", strlen(string1));
// Copy one string into a string buffer
strcpy(string2, string0);
printf("string2 = %s\n", string2);
// Copies only n characters from the beginning of one string to another
strncpy(string2, string0, 5);
printf("string2 = %s\n", string2);
// Concatenate strings
// string1 is appended to the end of string2
strcat(string2, string1);
printf("string2 = %s\n", string2);
// Find a substring
printf("strstr(string1, \"just\") -> %s\n", strstr(string1, "just"));
// Comparing strings
// strcmp gives (returns) value 0 if strings are equal. For meaning of other values, check strcmp manual.
int cmp0 = strcmp(string0, "Hello world!");
int cmp1 = strcmp("This is just another sad string\n", string1);
printf("Comparing string0 vs 'Hello world!' -> %d\n", cmp0);
printf("Comparing 'This is just another sad string\\n' vs string1 -> %d\n", cmp1);
strcpy(string2, string0);
int cmp2 = strcmp(string0, string2);
printf("Comparing string0 vs string2 -> %d\n", cmp2);
return 0;
}Code: Select all
char* s1;
char* s2 = "Hi there mateys!";
strcpy(s1, s2);
printf("%s = %s\n", s1, s2);Code: Select all
char* s1 = "Lolcats";
char* s2 = "Hi there mateys!";
strcpy(s1, s2);
printf("%s = %s\n", s1, s2);Code: Select all
char* s2 = "Hi there mateys!";
char* s1 = s2;
printf("%s = %s\n", s1, s2);Code: Select all
char s1[100];
char s2 = "Mommy";
char s3 = "Daddy";
strcpy(s1, s2);
strcat(s1, " and ");
strcat(s1, s3);
printf("%s\n", s1);- Make a program that shows a string and a new line without writing any \n character on the printf line.
- Make a program that concatenates 2 strings and shows the resulting string and its length.
- Make a program that shows the average length of 4 strings.
- Make a program that having the string "Hello dang world", manipulates it and prints "Hello world".
- Make a program that erases everything on a string after the string "me" (this one included).


