0% found this document useful (0 votes)
31 views

Strings: Eric Mccreath

Strings in C are arrays of characters that are terminated with a NULL or 0 character. The string library provides functions like strcpy() to copy strings and strlen() to determine the length of a string by searching for the NULL terminator. Exercises are provided to write functions to check if a string is a palindrome by reversing it and to count the number of words in a string.

Uploaded by

boletodon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Strings: Eric Mccreath

Strings in C are arrays of characters that are terminated with a NULL or 0 character. The string library provides functions like strcpy() to copy strings and strlen() to determine the length of a string by searching for the NULL terminator. Exercises are provided to write functions to check if a string is a palindrome by reversing it and to count the number of words in a string.

Uploaded by

boletodon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Strings

Eric McCreath
Strings
Strings are just arrays of characters.
char str[30];

You can initialize strings:


char name[] = "Eric";

You can also use these string literals as inputs to parameters for
functions:
if (strcmp("Bill",name) == 0) { ...

However you can NOT use assignment for copying strings:


str = "Fred"; // THIS WILL NOT WORK

Rather you should use the string library:


#include<string.h>
....
strcpy(str,"Fred");

2
Length
The end of a string is marked by a NULL or 0 character. Note that
generally within computing "null" is just another way of saying 0.
The length of a string is determined by searching through the string
until the zero is found. When allocating strings (which are just
arrays) one should allow space for this NULL .
The string library has a function you can use to determine the
length of string.
length = strlen(mystring);

3
Exersizes
Write a function that determines if a string is a palindrome (the
string remains the same when reversed).
Write a function that counts the number of words in a string.

You might also like