1 / 10

Programming Training

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:

morna
Download Presentation

Programming Training

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Training Main Points: - Strings and Characters in C.

  2. 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.

  3. 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.

  4. 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);

  5. 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);

  6. 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’;

  7. 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’;

  8. 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++;

  9. 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; }

  10. To do List • Solve the HW problems.

More Related