150 likes | 395 Views
STRING.H. C string functions! Gloria Szilasi. Strings in C . C-Style Strings are really arrays! Char string[50]; will declare a string for 49 letters. You are allowed to delete! Delete [] array ; They use pointer to! . Basic. printf (“My Name is”); // print a line
E N D
STRING.H C string functions! Gloria Szilasi
Strings in C • C-Style Strings are really arrays! • Char string[50]; will declare a string for 49 letters. • You are allowed to delete! • Delete [] array; • They use pointer to!
Basic • printf(“My Name is”); // print a line • scanf(“%s”,name);//reads in a STRING • %s for String • %d for Int • printf(“Your value is %d congratulations”,value); • Where %d is , value gets printed
String Functions The nine most commonly used functions (and their parameters) in the string library are:
String Functions • strcat - concatenate two strings • (Destination, source) • strncat - concatenate one string with part of another • (Destination, source, num) • Num=max number of characters to be appended • strchr - string scanning operation • (str, character) • Returns a pointer to first occurrence
String Functions continue… • strcmp - compare two strings • (str1, str2) • if( strcmp(mystring, yourstring) == 0) //zero indicates both are equal • strcpy - copy a string • (destination, source) • strncpy - copy part of a string • (destination, source, num) • Num=max # char copied from source
String Functions continue… • strlen - get string length • (string) • strncmp - compare parts of two strings • (st1,str2,num) • strrchr - string scanning operation • (str, character)
practice • Write a program in C using the #include<string.h> function’s that does the following: • Ask the user 1ST to enter in their last name. • Then enter in their first name. • Then concatenate the two strings using strncat(); • When conceiting it does not add a space.How can you do this? • Tell the user’s name length. • Ask user if the name they entered is correct. • ( hint: use a loop and strcmp)
If you have time…. • Check at what position a certain letter is in the array. • Example. My name is Gloria, and I want to know the position of the letter r. • Answer:4 • I just want to display my name as szilasiG. • So not print out my entire first name. • What function would I use?
Help. • http://www.cprogramming.com/tutorial/c/lesson9.html • http://www.cplusplus.com/reference/clibrary/cstring/