A string is a series of characters - "guitar", "truck", "earth".
Those are all strings. The way C represents strings is just as
an array of characters! Now that I explained the easy part, it
gets plenty hard :) As I said, a string is an array of characters,
so to declare one we could do.
char name[ 16 ];
this would be a char array (string) that can hold no more than 16
characters. If you try to put more in, you will be writing into
undeclared memory and bad things will happen. Also, C only lets
us fill one variable at a time - so at first glance we would have to do this:
char name[ 16 ];
name[ 0 ] = 'd';
name[ 1 ] = 'a';
name[ 2 ] = 'v';
name[ 3 ] = 'e';
which could take a very long time. Alternately,
we can fill the string when it is declared.
Example:
char name[ 16 ] = "Dave";
do it this way and the compiler will automatically fill it for us :)
Now notice, we have name declared as 16 chars, but really we are only using 4.
We have it declared bigger so we could put different things in there, like first
it might hold Dave, then it could hold "TrapZZ" later on. But how does the
computer know to only read elements 0 through 3 if it's "Dave" ? The answer is
called a null terminator. Essentially it's just the number 0 (which also means null)
at the end of the string. For instance, if we were to fill each variable
(like the first example) we would need to null terminate it...
name[ 0 ] = 'd';
name[ 1 ] = 'a';
name[ 2 ] = 'v';
name[ 3 ] = 'e';
name[ 4 ] = 0;
now when the computer goes to display it, it displays each letter until it comes
to the 0 and then it stops. When you declare it like this:
//the compiler will automatically put the null
//terminator after the "e" in "Dave".
char name[ 16 ] = "Dave";
So how do you print a string? You use the %s character in printf.
And to fill a string you use the %s in scanf.
For example:
char firstName[ 128 ];
scanf( "%s", firstName );
that code will accept someones input into the first name. Do not use
the addressof operator (&) before firstName in scanf. This is because it is
an array and an array already points to it's first address. (We briefly
covered that last tutorial but I want to make sure you don't forget!)
How do you compare strings? strcmp or strcmpi
(short for string compare). The strcmp compares strings exactly, strcmpi
compares strings and ignores the case of the letters (capital or lower case).
The strcmp functions will return a 0 if the strings are equal, otherwise it
returns > 0 if string 1 is greater or < 0 if string 2 is greater.
Try typing strcmp into your compiler and then hit f1
on it - you should get lots of helpful information!
Here is how to use it.
char myName[ 32 ] = "TrapZZ";
char secondName[ 32 ];
printf( "enter your first name: " );
scanf( "%s", secondName );
if ( strcmpi( myName, secondName ) == 0 )
{
printf( "the names are equal" );
}
else
{
printf( "the names are not equal" );
}
how do you copy strings? You use a function called strncpy
(short for string copy). It takes 2 parameters, a string to
copy to and the string to copy.
Example:
char myName[ 32 ] = "TrapZZ";
char secondName[ 32 ];
//this will write "TrapZZ" into the secondName char array
strncpy( secondName, myName );
ok! You must remember that strings are just char arrays. You can't
just do: secondName = myName. Because secondName and myName both
represent arrays and it doesn't know which element of the array to copy.
If you did just want to copy a single letter, you can specify an array element to copy:
secondName[ 0 ] = myName[ 0 ];
that will copy the 'T' into the first
element of secondName. But since secondName
has no null terminator it would crash if you
tried to print it out - because remember,
the computer prints out every element of the
array until it comes to a null terminator.
Since we didn't give a null terminator here
it would continue to print until it crashed.
To provide a null terminator you would do:
secondName[ 1 ] = 0;
now when you print secondName you would get a 'T'.
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( void )
{
char name[ 20 ];
char noun[ 20 ];
char adj[ 20 ];
char copyname[ 20 ];
printf( "\n Please enter your first name: " );
scanf( "%s", name );
printf( "\n How about a game %s.\n", name );
printf( "\n Enter a noun : " );
scanf("%s",noun);
printf( "\n Enter an adjective : " );
scanf( "%s", adj );
printf( "\n Your %s is very %s.\n", noun, adj );
//required header for strncpy is <string.h>
strncpy( copyname, name, 20 );
printf( "\n Thanks for playing %s.\n",copyname );
getch();
}
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( void )
{
char name[ 20 ];
char name2[ 20 ];
int i;
int j = 0;
printf( "\n Enter your name :" );
scanf( "%s", name );
for ( i = 0; name[ i ] != 0; i++ )
{
name2[ j ] = name[ i ];
j++;
}
//remember to null terminate so the
//computer knows to stop printing
name2[ j ] = 0;
printf( "\n %s is now copied to name2 \n", name2 );
printf( " without using strncpy!!" );
getch( );
}
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( void )
{
char name[ 20 ];
char name2[ 20 ];
int count = 0;
int i;
int j = 0;
printf( "\n Enter your name :" );
scanf( "%s", name );
//find out how long the name is
for ( i = 0; name[ i ] != 0; i++ )
{
count++;
}
for ( i = count - 1; i >= 0; i-- )
{
name2[ j ] = name[ i ];
j++;
}
//remember to null terminate so the
//computer knows to stop printing
name2[ j ] = 0;
printf( "\n %s is the reverse of %s", name2, name );
getch( );
}
__________________________________________________________
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main(void)
{
char password[ 8 ] = "AbCdEfG";
char login[ 32 ];
printf( "\n Enter your password: " );
scanf( "%s", login );
printf("\n (strcmp would catch the case");
printf( " sensitive password)" );
if ( strcmp( password, login ) == 0 )
{
printf( "\n The passwords are equal" );
}
else
{
printf( "\n The passwords are not equal" );
}
printf("\n\n (strcmpi would not catch the");
printf( " case sensitive password)" );
if ( strcmpi( password, login ) == 0 )
{
printf( "\n The passwords are equal" );
}
else
{
printf( "\n The passwords are not equal" );
}
getch();
}