a string is an array of characters. strings must have a 0 or null character after the last...

17
CHARACTER STRINGS IN C

Upload: ava-moyes

Post on 01-Apr-2015

253 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

CHARACTER STRINGS IN C

Page 2: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

A string is an array of characters. Strings must have a 0 or null character

after the last character to show where the string ends.

The null character is not included in the string.

Page 3: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

There are 2 ways of using strings. The first is with a character array and the second is with a string pointer.

Page 4: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

A character array is declared in the same way as a normal array.

char ca[10]; You must set the value of each individual element of the

array to the character you want and you must make the last character a 0. Remember to use %s when printing the string.

char ca[10];ca[0] = 'H';ca[1] = 'e';ca[2] = 'l';ca[3] = 'l';ca[4] = 'o';ca[5] = 0;printf("%s",ca);

Page 5: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

INITIALIZING STRINGS

char month1[ ]={‘j’,’a’,’n’,’u’,’a’,’r’,’y’};

/*String.c string variable*/ #include < stdio.h > main() { char month[15]; printf (“Enter the string”); fgets (month,8,stdin); printf (“The string entered is %s”, month); }

Page 6: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

String pointers are declared as a pointer to a char.

char *sp; When you assign a value to the string

pointer it will automatically put the 0 in for you unlike character arrays.

char *sp;sp = "Hello";printf("%s",sp);

Page 7: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

You can read a string into only a character array using scanf and not a string pointer.

If you want to read into a string pointer then you must make it point to a character array.

char ca[10],*sp;scanf("%s",ca);sp = ca;scanf("%s",sp);

Page 8: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

STRING HANDLING FUNCTIONS

The strings.h header file has some useful functions for working with strings. Here are some of the functions you will use most often:

Page 9: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

strcpy(destination,source)You can't just use string1 = string2 in C. You have to use the strcpy function to copy one string to another. strcpy copies the source string to the destination string.

s1 = "abc";s2 = "xyz";strcpy(s1,s2); // s1 = "xyz"

Page 10: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

strcat(destination,source)Joins the destination and source strings and puts the joined string into the destination string.

s1 = "abc";s2 = "xyz";strcat(s1,s2); // s1 = "abcxyz"

Page 11: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

strcmp(first,second)Compares the first and second strings. If the first string is greater than the second one then a number higher than 0 is returned. If the first string is less than the second then a number lower than 0 is returned. If the strings are equal then 0 is returned.

s1 = "abc";s2 = "abc";i = strcmp(s1,s2); // i = 0

Page 12: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

strlen(string)Returns the amount of characters in a string.

s = "abcde";i = strlen(s); // i = 5

Page 13: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

/* EXAMPLE PROGRAM TO USE STRING FUNCTIONS*/

#include < stdio.h > #include < string.h > void main() { char s1[20],s2[20],s3[20]; int x,l1,l2,l3; printf(“Enter the strings”); scanf(“%s%s”,s1,s2); x=strcmp(s1,s2); if(x!=0) {printf(“\nStrings are not equal\n”); strcat(s1,s2); } else printf(“\nStrings are equal”); strcpy(s3,s1); l1=strlen(s1); l2=strlen(s2); l3=strlen(s3); printf(“\ns1=%s\t length=%d characters\n”,s1,l1); printf(“\ns2=%s\t length=%d characters\n”,s2,l2); printf(“\ns3=%s\t length=%d characters\n”,s3,l3); }

Page 14: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

ADDITIONAL STRING FUNCTIONS

Converting strings to numbers char *str1=“123.79”; char *str2 = “3”; float x; int y;

x = atof(str1);y = atoi(str2);

Page 15: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

UPPERCASE

ctype.h has character handling functions

since a string is a character array, can convert to uppercase as follows:char name1[]=“Scooby”;int x;

for (x=0; x <=strlen(name1); x++)name1[x] = toupper(name1[x]);

Page 16: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

LOWERCASE

ctype.h has character handling functions

since a string is a character array, can convert to lowercase as follows:char name1[]=“Scooby”;int x;

for (x=0; x <=strlen(name1); x++)name1[x] = tolower(name1[x]);

Page 17: A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character

STRING PROGRAMMING ASSIGNMENTS1) Write a program that performs the following:

uses character arrays to read a user’s name from standard input (stdin)

tells the user how many characters are in their name displays the user’s name in uppercase displays the user’s name in lowercase changes the user’s name to append “man” on the end –

e.g. Jack becomes Jackman – and prints it out

2) Look up the strstr() function (page 196) and write a program that uses it to search for the last occurrence of a substring in a string. Print out the number of occurrences of the substring in the string.