100 likes | 187 Views
Programming Training. Main Points: - Strings and Characters in C. Working with Strings. Strings can be defined as: 1. a sequence of characters in between “…”. 2. an array of characters in which the last char is ‘’. String can be declared in two ways:
E N D
Programming Training Main Points: - Strings and Characters in C.
Working with Strings Strings can be defined as: 1. a sequence of characters in between “…”. 2. an array of characters in which the last char is ‘\0’. String can be declared in two ways: 1. as an array e.g. char str [200]; str does not change 2. as “pointer” e.g. char * str; str can change Reading a string can be done in several ways: printf(“%s”, &str); read the string until the first space. gets(str); read the whole string including spaces.
Operations with Strings Operations on strings are done with function from string.h. strcmp(str1,str2) compares two strings. strlen(str) gives the length of str strcpy(str1, str2) copy str2 to the str1 strstr(str1, str2) find the occurrence of str2 in str1 etc.
Working with Characters char is the C type for characters which can be letters, digits, special characters etc ctype.h is library to test the nature of chars. int isalnum(int c); int isalpha(int c);int iscntrl(int c); int isdigit(int c);int isgraph(int c); int islower(int c);int isprint(int c); int ispunct(int c);int isspace(int c); int isupper(int c);int isxdigit(int c); int tolower(int c); int toupper(int c);
Working with Characters char is the C type for characters which can be letters, digits, special characters etc ctype.h is library to test the nature of chars. int isalnum(int c); int isalpha(int c);int iscntrl(int c); int isdigit(int c);int isgraph(int c); int islower(int c);int isprint(int c); int ispunct(int c);int isspace(int c); int isupper(int c);int isxdigit(int c); int tolower(int c); int toupper(int c);
Example: Reverse a string Given a string str then write a function or a program to reverse that string. Input: str - string Output: reverse – string. Making a string requires: 1. Find the length of the string if possible. 2. Populating the characters. for(i=0;i<length;i++) { str[i] = a char value. } 3. terminating the string with \0 str[length] = ‘\0’;
Example: Reverse a string How to do it? 1. Traverse the string reverse forward and the string str backward simultaneously take the current character from str and place it in reverse for(i=0, j=length-1;i<length;i++, j--) { reverse[i] = str[j]; } 2. Terminate the string reverse. reverse[length] = ‘\0’;
Example: Counting spaces Given a string str then write a function or a program to find the number spaces are in the string. [It is a counting problem] Input: str - string Output: nr – int. How to do it? • Initialise nr = 0. • Traverse the string and test if the current character is a space repeat for i=0, 1, …, strlen if str[i] is space then nr++;
Example: Counting spaces int count_space (char * str) { int nr, i, length; length = strlen(str); nr = 0; for(i=0;i<n;i++) { if(isspace(str[i])) { nr++; } } return nr; }
To do List • Solve the HW problems.