Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C programming

Similar presentations


Presentation on theme: "Introduction to C programming"— Presentation transcript:

1 Introduction to C programming
String

2 What are strings in C? Strings in C are represented by arrays of characters. The end of the string is marked with a special character, the null character, which is simply the character with the value 0 (or \0 ). For example: char name[50] = “DAVE”;

3 What are strings in C? (Cont)
char a[50] = “This is \n a string.”; printf("This is a string value. Beep! Beep! \7\7"); 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... T h i s \n a t r I n g . \0

4 Conventions What output of printf(a); if a is:
What is the length of string a? What if char a[50] does not contain ‘\0’ at all? Are following correct, given char a[6]; a[6]=‘7’; a[5]=“7”; a[5]=7; a++; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ... T h i s \0 a t r I n g .

5 Declarations and Initializing strings
Array of char: char str[5] = {'l', 'i', 'n', 'u', 'x'}; char str[6] = {'l', 'i', 'n', 'u', 'x', '\0'}; Declarations

6 Declarations and Initializing strings
#include <stdio.h> #include <string.h> int main() { /* Example 1 */ char string1[ ] = "A string declared as an array.\n"; /* Example 2 */ char *string2 = "A string declared as a pointer.\n"; /* Example 3 */ char string3[30]; strcpy(string3, "A string constant copied in.\n"); printf (string1); printf (string2); printf (string3); return 0; }

7 Declarations and Initializing strings
char string1[] = "A string declared as an array.\n"; This is usually the best way to declare and initialize a string. The character array is declared explicitly. There is no size declaration for the array; just enough memory is allocated for the string, because the compiler knows how long the string constant is. The compiler stores the string constant in the character array and adds a null character (\0) to the end.

8 Declarations and Initializing strings
char *string2 = "A string declared as a pointer.\n"; The second of these initializations is a pointer to an array of characters. Just as in the last example, the compiler calculates the size of the array from the string constant and adds a null character. The compiler then assigns a pointer to the first character of the character array to the variable string2. Note: Most string functions will accept strings declared in either of these two ways.

9 Declarations and Initializing strings
char string3[30]; Declaring a string in this way is useful when you don't know what the string variable will contain, but have a general idea of the length of its contents (in this case, the string can be a maximum of 30 characters long). The drawback is that you will either have to use some kind of string function to assign the variable a value, as the next line of code does ( strcpy(string3, "A string constant copied in.\n");), or you will have to assign the elements of the array the hard way, character by character.

10 String Array A string array is an array of strings, which, of course, are themselves arrays of characters; in effect, a string array is a two-dimensional character array.

11 char *menu[] = { " ", " | MENU |", " | ~~~~~~~~~~~~ |", " | (0) Edit Preferences |", " | (1) Print Charge Sheet |", " | (2) Print Log Sheet |", " | (3) Calculate Bill |", " | (q) Quit |", " | |", " | Please enter ur choice. |", " “ }; int main() { int line; for (line = 0; line < 13; line++) printf ("%s\n", menu[line]); } return 0;

12 Troubles Because C has no built-in facilities for manipulating entire arrays (copying them, comparing them, etc.), it also has very few built-in facilities for manipulating strings: char firstname[50],lastname[50],fullname[100]; firstname= "Arnold"; /* Illegal */ lastname= "Schwarznegger"; /* Illegal */ fullname= "Mr"+firstname +lastname; /* Illegal */

13 String library functions
The GNU C Library provides a number of very useful functions which handle strings. Here is a list of the more common ones. To use the functions beginning with ato, you must include the header file stdlib.h; to use the functions beginning with str, you must include the header file string.h.

14 String library functions(1)
int atoi(const char *nptr); double atof(const char *nptr); long atol(const char *nptr);

15 String library functions(1)
/*atof Converts an ASCII string to its floating-point equivalent; for example, converts to the value */ #include <stdio.h> #include <stdlib.h> int main() { double my_value; char my_string[] = " "; my_value = atof(my_string); printf("%f\n", my_value); return 0; } The output from the above code is

16 String library functions(1)
/* atoi Converts an ASCII string to its integer equivalent; for example, converts to the value -23. */ int my_value; char my_string[] = "-23.5"; my_value = atoi(my_string); printf("%d\n", my_value); /*atol Converts an ASCII string to its long integer equivalent; for example, converts to the value */ long my_value; char my_string[] = " "; my_value = atol(my_string); printf("%ld\n", my_value);

17 String library functions(2)
strcpy strcpy copies a string, including the null character terminator from the source string to the destination. This function returns a pointer to the destination string, or a NULL pointer on error. Its prototype is: char *strcpy(char *dst, const char *src); strncpy strncpy is similar to strcpy, but it allows the number of characters to be copied to be specified. If the source is shorter than the destination, than the destination is padded with null characters up to the length specified. This function returns a pointer to the destination string, or a NULL pointer on error. Its prototype is: char *strncpy(char *dst, const char *src, size_t len);

18 String library functions(2)
strcat This function appends a source string to the end of a destination string. This function returns a pointer to the destination string, or a NULL pointer on error. Its prototype is: char *strcat(char *dst, const char *src); strncat This function appends at most N characters from the source string to the end of the destination string. This function returns a pointer to the destination string, or a NULL pointer on error. Its prototype is: char *strncat(char *dst, const char *src, size_t N);

19 String library functions(2)
strcmp This function compares two strings. If the first string is greater than the second, it returns a number greater than zero. If the second string is greater, it returns a number less than zero. If the strings are equal, it returns 0. Its prototype is: int strcmp(const char *first, const char *second); strncmp This function compares the first N characters of each string. If the first string is greater than the second, it returns a number greater than zero. If the second string is greater, it returns a number less than zero. If the strings are equal, it returns 0. Its prototype is: int strncmp(const char *first, const char *second, size_t N);

20 String library functions(2)
The strcmp() function lexically compares the two input strings and returns: Less than zero -- if string1 is lexically less than string2 Zero -- if string1 and string2 are lexically equal Greater than zero -- if string1 is lexically greater than string2

21 String library functions(2)
strlen This function returns the length of a string, not counting the null character at the end. That is, it returns the character count of the string, without the terminator. Its prototype is: size_t strlen(const char *str);

22 String library functions(2)
int strcasecmp(const char *s1, const char *s2) -- case insensitive version of strcmp(). int strncasecmp(const char *s1, const char *s2, int n) -- case insensitive version of strncmp().

23 String library functions(2)
To use any of these functions in your code, the header file "strings.h" must be included. It is the programmer's responsibility to check that the destination array is large enough to hold either what is copied or appended to it. C performs no error checking in this respect. See the array tutorial for more details. The data type size_t is equivalent to unsigned integer.

24 String library functions(2)
The use of most of the functions is straightforward, for example: char *str1 = "HELLO"; char *str2; int length; length = strlen("HELLO"); /* length = 5 */ (void) strcpy(str2,str1);

25 String library functions(2)
The strncat(), strncmp,() and strncpy() copy functions are string restricted version of their more general counterparts. They perform a similar task but only up to the first n characters. Note the the NULL terminated requirement may get violated when using these functions, for example: char *str1 = "HELLO"; char *str2; int length = 2; (void) strcpy(str2,str1, length); /* str2 = "HE" */ str2 is NOT NULL TERMINATED!! -- BEWARE

26 String library functions(3)
char *strchr(const char *string, int c) -- Find first occurrence of character c in string. char *strrchr(const char *string, int c) -- Find last occurrence of character c in string. char *strstr(const char *s1, const char *s2) -- locates the first occurrence of the string s2 in string s1. char *strpbrk(const char *s1, const char *s2) -- returns a pointer to the first occurrence in string s1 of any character from string s2, or a null pointer if no character from s2 exists in s1

27 String library functions(3)
size_t strspn(const char *s1, const char *s2) -- returns the number of characters at the begining of s1 that match s2. size_t strcspn(const char *s1, const char *s2) -- returns the number of characters at the begining of s1 that do not match s2. char *strtok(char *s1, const char *s2) -- break the string pointed to by s1 into a sequence of tokens, each of which is delimited by one or more characters from the string pointed to by s2. char *strtok_r(char *s1, const char *s2, char **lasts) -- has the same functionality as strtok() except that a pointer to a string placeholder lasts must be supplied by the caller.

28 Memory Operations: <memory.h>
void *memchr (void *s, int c, size_t n) -- Search for a character in a buffer . int memcmp (void *s1, void *s2, size_t n) -- Compare two buffers. void *memcpy (void *dest, void *src, size_t n) -- Copy one buffer into another . void *memmove (void *dest, void *src, size_t n) -- Move a number of bytes from one buffer lo another. void *memset (void *s, int c, size_t n) -- Set all bytes of a buffer to a given character.

29 String Input & Output Output: Input: File operations
printf(“This is a string: %s”, str1); Input: scanf(“%s%ld%lf%s”, str1, &value1, &value2, str2); gets scanline File operations fopen, fclose, fgets…


Download ppt "Introduction to C programming"

Similar presentations


Ads by Google