Learn About Strings in C++ Language

s trings in c langauge n.w
1 / 4
Embed
Share

Explore the fundamentals of strings in C++ programming language, including string manipulation, common errors to avoid, and essential string functions. Discover how to write a C++ program to print each word on a different line from a long string.

  • C++
  • Programming
  • String Manipulation
  • C++ Functions

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. STRINGS IN C++ LANGAUGE Setting By : A. L. Muhannad Ridha Eleven Lecture

  2. 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', 'L', 'O'}; A common programming error results from confusing a single-character string (e.g., "A") with a single character (e.g., 'A'). These two are not equivalent. (the character 'A' followed by the character '\0') 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

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

  4. 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="MUHANNAD RIDHA"; for(int i=0;i<strlen(str);i++) if(str[i]==' ') cout<<endl; else cout<<str[i]; } Output: -------------- MUHANNAD RIDHA

Related


More Related Content