C++/String/char array string
Содержание
- 1 Convert char array to upper case
- 2 Count spaces, punctuation, digits, and letters.
- 3 Demonstrate the basic null-terminated string functions.
- 4 Filling an Array
- 5 Get the string length
- 6 Operator pointer
- 7 Use strlen() to get the length of a char array buffer
- 8 Using atoi() function
- 9 Using strcat() and strncat().
- 10 Using strcpy and string terminator
- 11 Using strcpy() to assign value from one char array to another char array
- 12 Using strncpy() and string terminator
- 13 Using strncpy() to assign one char array to another char array
Convert char array to upper case
#include <iostream>
#include <string.h>
using namespace std;
struct Msg
{
char message[256];
void show_message(void) { cout << message; }
};
struct UpperMsg
{
char message[256];
void show_message(void) { cout << strupr(message); }
};
int main(void)
{
Msg book = { "B" };
UpperMsg book_upr = { "c" };
book.show_message();
book_upr.show_message();
}
Count spaces, punctuation, digits, and letters.
#include <iostream>
#include <cctype>
using namespace std;
int main() {
const char *str = "This is a test. 1 2 3 4 5";
int letters = 0, spaces = 0, punct = 0, digits = 0;
cout << str << endl;
while(*str) {
if(isalpha(*str))
++letters;
else if(isspace(*str))
++spaces;
else if(ispunct(*str))
++punct;
else if(isdigit(*str))
++digits;
++str;
}
cout << "Letters: " << letters << endl;
cout << "Digits: " << digits << endl;
cout << "Spaces: " << spaces << endl;
cout << "Punctuation: " << punct << endl;
return 0;
}
Demonstrate the basic null-terminated string functions.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char strA[7] = "U";
char strB[5] = "D";
char strC[5] = "L";
char strD[6] = "R";
cout << "Here are the strings: " << endl;
cout << "strA: " << strA << endl;
cout << "strB: " << strB << endl;
cout << "strC: " << strC << endl;
cout << "strD: " << strD << "\n\n";
cout << "Length of strA is " << strlen(strA) << endl;
strcat(strA, strB);
cout << "strA after concatenation: " << strA << endl;
cout << "Length of strA is now " << strlen(strA) << endl;
strcpy(strB, strC);
cout << "strB now holds: " << strB << endl;
if(!strcmp(strB, strC))
cout << "strB is equal to strC\n";
int result = strcmp(strC, strD);
if(!result)
cout << "strC is equal to strD\n";
else if(result < 0)
cout << "strC is less than strD\n";
else if(result > 0)
cout << "strC is greater than strD\n";
return 0;
}
Filling an Array
#include <iostream>
using namespace std;
int main(){
char buffer[80] = {"\0"};
std::cout << "Enter the string: ";
std::cin >> buffer;
std::cout << "Here"s the buffer: " << buffer << std::endl;
return 0;
}
Get the string length
#include <iostream>
#include <string.h>
using namespace std;
main(void)
{
int i;
i = 10;
int j = 100;
cout << i*j << "\n";
cout << "Enter a string: ";
char str[80];
cin >> str;
// display the string in reverse order
int k;
k = strlen(str);
k--;
while(k>=0) {
cout << str[k];
k--;
}
return 0;
}
Operator pointer
#include <iostream>
#include <string.h>
using namespace std;
class Book {
public:
Book(char *title, char *publisher, char *author);
void show_book(void) {
cout << "Book: " << title << " by " <<
author << " Publisher: " << publisher << endl;
};
operator char *();
private:
char title[64];
char author[64];
char publisher[64];
};
Book::Book(char *title, char *publisher, char *author)
{
strcpy(Book::title, title);
strcpy(Book::publisher, publisher);
strcpy(Book::author, author);
}
Book::operator char *(void)
{
char *ptr = new char[256];
return(strcpy(ptr, title));
}
int main(void)
{
Book myBook("A", "B","C");
char *title;
title = myBook;
cout << "The book"s title is " << title << endl;
}
Use strlen() to get the length of a char array buffer
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char buffer[80];
do
{
cout << "Enter a string up to 80 characters: ";
cin.getline(buffer,80);
cout << "Your string is " << strlen(buffer);
cout << " characters long." << endl;
}while (strlen(buffer));
return 0;
}
Using atoi() function
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
char buffer[80];
cout << "Enter a number: ";
cin >> buffer;
int number;
number = atoi(buffer);
cout << "Here"s the number: " << number << endl;
int sum = atoi(buffer) + 5;
cout << "Here"s sum: " << sum << endl;
return 0;
}
Using strcat() and strncat().
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char stringOne[255];
char stringTwo[255];
stringOne[0]="\0";
stringTwo[0]="\0";
cout << "Enter a string: ";
cin.getline(stringOne,80);
cout << "Enter a second string: ";
cin.getline(stringTwo,80);
cout << "String One: " << stringOne << endl;
cout << "String Two: " << stringTwo << endl;
strcat(stringOne," ");
strncat(stringOne,stringTwo,10);
cout << "String One: " << stringOne << endl;
cout << "String Two: " << stringTwo << endl;
return 0;
}
Using strcpy and string terminator
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char stringOne[80];
char stringTwo[80];
stringOne[0]="\0";
stringTwo[0]="\0";
cout << "String One: " << stringOne << endl;
cout << "String Two: " << stringTwo << endl;
cout << "Enter a string: ";
cin.getline(stringOne,80);
cout << "\nString One: " << stringOne << endl;
cout << "String Two: " << stringTwo << endl;
cout << "copying..." << endl;
strcpy(stringTwo,stringOne);
cout << "\nString One: " << stringOne << endl;
cout << "String Two: " << stringTwo << endl;
cout << "\nDone " << endl;
return 0;
}
Using strcpy() to assign value from one char array to another char array
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char String1[] = "this is a test";
char String2[80] = {"\0"};
strcpy(String2,String1);
cout << "String1: " << String1 << endl;
cout << "String2: " << String2 << endl;
return 0;
}
Using strncpy() and string terminator
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char stringOne[80];
char stringTwo[10];
char stringThree[80];
stringOne[0]="\0";
stringTwo[0]="\0";
stringThree[0]="\0";
cout << "String One: " << stringOne << endl;
cout << "String Two: " << stringTwo << endl;
cout << "String Three: " << stringThree << endl;
cout << "Enter a long string: ";
cin.getline(stringOne,80);
strcpy(stringThree,stringOne);
cout << "\nString One: " << stringOne << endl;
cout << "String Two: " << stringTwo << endl;
cout << "String Three: " << stringThree << endl;
strncpy(stringTwo,stringOne,9);
cout << "\nString One: " << stringOne << endl;
cout << "String Two: " << stringTwo << endl;
cout << "String Three: " << stringThree << endl;
stringTwo[9]="\0";
cout << "\nString One: " << stringOne << endl;
cout << "String Two: " << stringTwo << endl;
cout << "String Three: " << stringThree << endl;
cout << "\nDone." << endl;
return 0;
}
Using strncpy() to assign one char array to another char array
#include <iostream>
#include <string.h>
int main()
{
const int MaxLength = 80;
char String1[] = "this is a test";
char String2[MaxLength+1] = {"\0"};
strncpy(String2, String1, MaxLength);
std::cout << "String1: " << String1 << std::endl;
std::cout << "String2: " << String2 << std::endl;
return 0;
}