C++ Tutorial/Data Types/char array

Материал из C\C++ эксперт
Перейти к: навигация, поиск

char array buffers

#include <iostream>
 
 int main()
 {
     char buffer[80];
     std::cout << "Enter the string: ";
     std::cin >> buffer;
     std::cout << "Here"s the buffer:  " << buffer << std::endl;
     return 0;
 }
Enter the string: string
Here"s the buffer:  string

Copying a string using array notation

#include <iostream>
using std::cout;
using std::endl;
void copy1( char *, const char * );
int main()
{
   char string1[ 10 ];
   char *string2 = "Hello";
   copy1( string1, string2 );
   cout << "string1 = " << string1 << endl;
   return 0;
}
void copy1( char * s1, const char * s2 )
{
   for ( int i = 0; ( s1[ i ] = s2[ i ] ) != "\0"; i++ )
      ;
}
string1 = Hello

Copying a string using pointer notation

#include <iostream>
using std::cout;
using std::endl;
void copy( char *, const char * );
int main()
{
   char string1[ 10 ];
   char string2[] = "Good Bye";
   copy( string1, string2 );
   cout << "string1 = " << string2 << endl;
   return 0;
}
void copy( char *s1, const char *s2 )
{
   for ( ; ( *s1 = *s2 ) != "\0"; s1++, s2++ )
      ;
}
string1 = Good Bye

Get line with buffer size for char array reading

#include <iostream>
#include <string>
using namespace std;
const int kBufferSize = 1024;
int main(int argc, char** argv)
{
  char buffer[kBufferSize + 1];
  cin.getline(buffer, kBufferSize);
  cout << buffer << endl;
  string myString;
  std::getline(cin, myString);
  cout << myString << endl;
}

initialized string

#include <iostream>  
  using namespace std;  
    
  int main(){  
     char str[] = "this is a test";  
     cout << str << endl;  
     return 0;  
  }

Initializing char pointers with strings

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main() {
  const char* pstar1 = "AAA";
  const char* pstar2 = "BBBB";
  const char* pstar3 = "CCCCC";
  const char* pstar4 = "DDDDDD";
  const char* pstar5 = "EEEEEEE";
  const char* pstar6 = "FFFFFFFF";
  const char* pstr   = "GGGGGGGGG";
  cout << pstar1;
  cout << pstar2;
  cout << pstar3;
  cout << pstar4;
  cout << pstar5;
  cout << pstar6;
  return 0;
}
AAABBBBCCCCCDDDDDDEEEEEEEFFFFFFFF

multidimensional char arrays

#include <iostream>
using namespace std;
int main()
{
    const int ROWS = 3;
    const int COLUMNS = 3;
    char board[ROWS][COLUMNS] = { {"O", "X", "O"},
                                  {" ", "X", "X"},
                                  {"X", "O", "O"} };
    for (int i = 0; i < ROWS; ++i)
    {
        for (int j = 0; j < COLUMNS; ++j)
            cout << board[i][j];
        cout << endl;
    }
    board[1][0] = "X";
    for (int i = 0; i < ROWS; ++i)
    {
         for (int j = 0; j < COLUMNS; ++j)
             cout << board[i][j];
         cout << endl;
    }
    return 0;
}

Read from keyboard and output char array

#include <iostream>
using namespace std; 
int main(void){
   int age;
   float salary;
   char name[128];
   cout << "Enter your first name age salary: ";
   cin >> name >> age >> salary;
   cout << name << " " << age << " " << salary;
}

reads multiple lines, terminates on "$" character

#include <iostream>  
  using namespace std;  
    
  const int MAX = 2000;  
  char str[MAX];         
    
  int main(){  
     cout << "\nEnter a string:\n";  
     cin.get(str, MAX, "$");
     cout << "You entered:\n" << str << endl;  
     return 0;  
  }

reads string with embedded blanks

#include <iostream>  
  using namespace std;  
    
  int main(){  
     const int MAX = 80;
     char str[MAX]; 
    
     cout << "\nEnter a string: ";  
     cin.get(str, MAX); 
     cout << "You entered: " << str << endl;  
     return 0;  
  }

Reverse a string in place.

#include <iostream> 
#include <cstring> 
using namespace std; 
 
int main() 
{ 
  char str[] = "this is a test"; 
  char *start, *end; 
  int len; 
  char t; 
 
  cout << "Original: " << str << "\n"; 
   
  len = strlen(str); 
 
  start = str; 
  end = &str[len-1]; 
 
  while(start < end) { 
    // swap chars 
    t = *start; 
    *start = *end; 
    *end = t; 
 
    // advance pointers 
    start++; 
    end--; 
  } 
 
 
  cout << "Reversed: " << str << "\n"; 
  return 0; 
}
Original: this is a test
Reversed: tset a si siht

Reverse case using array indexing.

#include <iostream> 
#include <cctype> 
using namespace std; 
 
int main() 
{ 
  int i; 
  char str[80] = "This Is A Test"; 
 
  cout << "Original string: " << str << "\n"; 
 
  for(i = 0; str[i]; i++) { 
    if(isupper(str[i])) 
      str[i] = tolower(str[i]); 
    else if(islower(str[i])) 
      str[i] = toupper(str[i]); 
  } 
 
  cout << "Inverted-case string: " << str; 
 
  return 0; 
}
Original string: This Is A Test
Inverted-case string: tHIS iS a tEST

simple string variable

#include <iostream>  
  using namespace std;  
    
  int main(){  
     const int MAX = 80;
     char str[MAX];     
    
     cout << "Enter a string: ";  
     cin >> str;        
                        
     cout << "You entered: " << str << endl;  
     return 0;  
  }

Treating character arrays as strings.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
   char string1[ 20 ]; 
   char string2[] = "string literal"; 
   cout << "Enter the string: ";
   cin >> string1; 
   cout << "string1 is: " << string1 << "\nstring2 is: " << string2;
    for ( int i = 0; string1[ i ] != "\0"; i++ )
      cout << string1[ i ] << " ";
   cin >> string1; 
   cout << "\nstring1 is: " << string1 << endl;
   return 0; 
}
Enter the string: string
string1 is: string
string2 is: string literals t r i n g
string
string1 is: string

Use cin.get() to read a string based on char array

#include <iostream>
 
 int main()
 {
     char buffer[80];
     std::cout << "Enter the string: ";
     std::cin.get(buffer, 79);       // get up to 79 or newline
     std::cout << "Here"s the buffer:  " << buffer << std::endl;
     return 0;
 }
Enter the string: string
Here"s the buffer:  string

Using an array of pointers to char

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main() {
  const char* pstars[] = {"A","BB","CCC","DDDD","EEEEE","FFFFFF"};
  cout << pstars[2];
  cout << endl;
  return 0;
}
CCC