STRINGS WITH STRING FUNCTIONS IN C LANGAUGE Setting

  • Slides: 9
Download presentation
STRINGS WITH STRING FUNCTIONS IN C++ LANGAUGE Setting By : A. L. Waleed Rasheed

STRINGS WITH STRING FUNCTIONS IN C++ LANGAUGE Setting By : A. L. Waleed Rasheed

Strings: ﺍﻟﺴﻼﺳﻞ ﺍﻟﺤﺮﻓﻴﺔ A string is a consecutive sequence (i. e. , array) of

Strings: ﺍﻟﺴﻼﺳﻞ ﺍﻟﺤﺮﻓﻴﺔ A string is a consecutive sequence (i. e. , array) of characters which are terminated by a null character. char *str = "HELLO"; ﺗﻌﺮﻳﻒ ﺍﻟﺴﻠﺴﺔ ﺍﻟﺤﺮﻓﻴﺔ char str[] = "HELLO"; ﺃﻮ ﺗﻌﺮﻑ ﺑﺼﻴﻐﺔ ﺍﻟﻤﺼﻔﻮﻓﺔ defines str to be an array of six characters: five letters and a null character. char str[] = {'H', 'E', 'L', 'O'}; ﺗﻌﺮﻳﻒ ﺍﻟﺴﻠﺴﻠﺔ ﻛﻤﺼﻔﻮﻓﺔ ﺭﻣﻮﺯ ﺗﻠﺤﻖ ﺑﺮﻣﺰ ﺍﻟﻨﻬﺎﻳﺔ A common programming error results from confusing a singlecharacter string (e. g. , "A") with a single character (e. g. , 'A'). These two are not equivalent. (the character 'A' followed by the character '') A long string may extend beyond a single line, terminated by a backslash. For example: "Example to show the use of backslash for writing a long string”

String Functions: The standard library string : <string. h> Function strlen(s) Mean Function Calculates

String Functions: The standard library string : <string. h> Function strlen(s) Mean Function Calculates length of string s Compare one string into another char *s = "HELLO"; If s 1 less than s 2 return value <0 cout<<strlen(s); Strcmp(s 1, s 2) If s 1 the same as s 2 output: 5 return value ==0 Appends one string to another If s 1 greater than s 2 char *s 1 = "HELLO"; Strcat(s 1, s 2) return value >0 char *s 2 = " WORLD"; Convert uppercase letters in string to cout<<strcat(s 1, s 2); output: Hello World lowercase letters. Copies one string into another char *s = "HELLO"; char *s 1 = "HELLO"; Strcpy(s 1, s 2) Mean char *s 2 = " WORLD"; cout<<strcpy(s 1, s 2); output: WORLD strlwr(s) cout<<strlwr(s); output: hello

Q//write program a C++ to print each word on different line from long string?

Q//write program a C++ to print each word on different line from long string? #include<iostream. h> #include<string. h> void main() { char *str="WALEED RASHEED"; for(int i=0; i<strlen(str); i++) if(str[i]==' ')cout<<endl; else cout<<str[i]; } Output: -------WALEED RASHEED

Q\write program a C++ to print each letters on different line from long string?

Q\write program a C++ to print each letters on different line from long string? #include<iostream. h> #include<string. h> void main() { char *str="WALEED RASHEED"; int i=0; while(str[i]!='') { cout<<str[i]<<endl; i++; } } Output: -------W A L E E D R A S H E E D

Q//write this output : #include<iostream. h> main() { // char str[] = "HELLO"; char

Q//write this output : #include<iostream. h> main() { // char str[] = "HELLO"; char str[] = {'H', 'E', 'L', 'O'}; int i, j; for ( i = 0; i < 5; ++i) { for ( j = 0; j <= i; ++j) cout<<str[j]; cout<<endl; } } Output : H HE HELLO

#include<iostream. h> #include<string. h> #include<stdio. h> ﺍﺳﻢ ﺍﻟﻤﻜﺘﺒﺔ void main() ﺍﻟﺨﺎﺹ ﺑﺎﻳﻌﺎﺯ { ﻗﺮﺍﺀﺓ

#include<iostream. h> #include<string. h> #include<stdio. h> ﺍﺳﻢ ﺍﻟﻤﻜﺘﺒﺔ void main() ﺍﻟﺨﺎﺹ ﺑﺎﻳﻌﺎﺯ { ﻗﺮﺍﺀﺓ char str[80]; ﺍﻳﻌﺎﺯ ﻗﺮﺍﺀﺓ gets(str); ﺍﻟﺴﻠﺴﺔ ﺍﻟﺤﺮﻓﻴﺔ int i=0; while(str[i]!='') { cout<<str[i]<<endl; i++; } } Input: ----WALEED RASHEED Output: ------- W A L E E D R A S H E E D

Homework Q 1/ Write a program to read a string and count the number

Homework Q 1/ Write a program to read a string and count the number of words? Q 2/ Write a program to read a string and find number of repeat “q” or “Q” letter?