Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Problem Solving and Programming

Similar presentations


Presentation on theme: "Introduction to Problem Solving and Programming"— Presentation transcript:

1 Introduction to Problem Solving and Programming
Lecture 7 – Strings

2 Strings A string is any sequence of characters enclosed in double quotes “Hello World". There is no separate data type for strings as char, integer, float or double. Instead, a string is represented in C as an array of type char.

3 Strings We can declare and initialize a string variable using any of the following: char str1[20]={'H', 'e','l','l','o',' ','W','o','r','l','d','\0'}; //as other arrays char str2[20] = “Hello World"; /*with size specified explicitly */ char str3[]= “Hello World"; /*with size specified implicitly */ The string is terminated by the \0 character:

4 Strings For example, the declaration: char str[20] = “Hello World";
is actually represented in the memory as shown below: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 H e l o W r d \0

5 Input / Output of strings
We can use %s to read and print a string printf (“Name is %s \n”, Name); scanf (“%s”, str); Pointer to character array

6 Input / Output of strings
We can use %s to read and print a string printf (“Name is %s \n”, Name); scanf (“%s”, str); Where is &

7 Input / Output with gets and puts
A problem with scanf when reading a string is that it stops scanning the moment it encounters a white space. Thus, it cannot scan a string such as: “Ali Zaki Street” in one variable. An alternative to scanf is the function gets that takes a string variable as argument. char school[SIZE]; gets(school);

8 Input / Output with gets and puts
A problem with scanf when reading a string is that it stops scanning the moment it encounters a white space. Thus, it cannot scan a string such as: “Ali Zaki Street” in one variable. An alternative to scanf is the function gets that takes a string variable as argument. char school[SIZE]; gets(school); No &

9 Input / Output with gets and puts
The gets function continues to scan for characters until it encounters the new line character – until the user types the Enter key. The function puts can be used to print a string. puts(school);

10 String.h To use some operations or functions on strings, we need to include file string.h #include<string.h> So, we can use functions strcpy(s1, s2) strlen(s) strcmp(s1, s2) strcat (s1, s2)

11 String Functions strcpy(s1, s2)
we can not use operator “ = “ with string strcpy is copying s2 into s1 (s1  s2)

12 Strcpy Array style void strcpy(char dest[], char src[]) { int i = 0; while (src[i] != ‘\0’) { dest[i] = src[i]; i++; } dest[i] = ‘\0’

13 Strcpy Pointer style void strcpy(char* dest, char* src) { while (*src != ‘\0’) { *dest = *src; dest++; src++; }

14 String Functions strlen(S) Return the length of string S
strcpy(S, “industrial robot”); strlen(S) will return 16 Note the difference between this and: char s[]=“industrial robot” ;

15 Strlen Array style int strlen(char str[]) { int i = 0;
while (str[i] != ‘\0’) { i++; } return i;

16 Strlen Pointer style int strlen(char* str) { int i = 0; while (*str != ‘\0’) { i++; str++; } return i;

17 String Functions strcmp(s1, s2) It is for comparison of s1 and s2
It return 0 if s1=s2 Return –ve if s1 is less than s2 Return +ve if s1 is greater than s2

18 String Functions It appends/concatenates s2 to the end of s1
strcat(s1, s2) It appends/concatenates s2 to the end of s1 Example: s1= “Lion” , s2 = “King” strcat(s1,s2)  s1=“LionKing”

19 Example (1) #include <stdio.h> #include <string.h>
int main() { int k; char src [30]; char dest [10]=" Tutorial"; strcpy(src, "This is a"); strcat(src, dest); k=strlen(src); printf("Final string : %s, with length = %d\n", src,k); return(0); }

20 Example (2) /* Compare two strings and determine which is greater*/
gets(s); gets(d); k=strcmp(s,d); if (k<0) printf("\n First is less than second \n"); else if (k==0) printf("\n First equals second \n"); else if (k>0) printf("\n First is greater than second \n");

21 Example (3) Write a function to reverse a string.
Write a program to check if a given string is a “palindrome”. Discuss alternative approaches, one of which you can use the reverse function.

22 Array of Strings To represent arrays of strings we need 2- dimensional arrays of characters. The first-dimension represents the number of strings in the array and the second-dimension represents the strings. The following are statements to declare an array to store up to 30 names, each of maximum length, 25 characters. #define NUM_NAMES 30 #define NAME_LEN 25 ... char names[NUM_NAMES][NAME_LEN];

23 Array of Strings We can also initialize an array of strings at declaration in the following manner: char month[12][10] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

24 Example // Print array of strings #include <stdio.h> int main()
{ int i; char Names[3][20]={"Akram Ali“ , “Mido Zaki", , ”Said Abdo”}; for (i=0; i<3; i++) printf("%s\n",Names[i]); return(0);}

25 Array of Pointers to strings
For example: an array of strings char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; Strings are pointers to the first character Each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored suit array has a fixed size, but strings can be of any size

26 Arrays of Pointers to strings
char * – each element of suit is a pointer to a char The strings are not actually stored in the array suit, only pointers to the strings are stored suit array has a fixed size, but strings can be of any size suit[3] suit[2] suit[1] suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’\0’ ’D’ ’i’ ’m’ ’o’ ’n’ ’d’ ’C’ ’l’ ’u’ ’b’ ’S’ ’p’

27 Example (Array of strings)
char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" }; int main() { int x ; for (x = 0; x < 4 ; x++) printf("\n %s %d ", suit[x],strlen(suit[x])); return 0; }

28 Questions?


Download ppt "Introduction to Problem Solving and Programming"

Similar presentations


Ads by Google