簡單字符串的輸入輸出

#include<iostream>
#include<cstdlib>//裏面封裝了許多函數,
                 //例如malloc,free這些動態分配內存函數等等,
                 //如果你不引入這個庫,這些函數就無法使用
using namespace std;
int main()
{
    int i;
    char str[30];
    char str1[30];
    cout<<"請輸入字符串:";
    cin>>str;
    cout<<"輸入的字符串爲:" <<str<<endl;
    for(i=0;str[i]!='\0';i++)//開始遍歷,當字符串裏面爲空時,for結束
    {
        str1[i]=str[i];//逐一行字符拷貝 
    }
    str1[i]='\0';//在計算字符串長度時,並不包含字符串結尾字符'\0'
    //這句話其實我挺不明白的 
    cout<<"此字符串有"<<i<<"個字符"<<endl;
     cout<<"原字符串str="<<str<<endl;
     cout<<"複製後字符串str1="<<str1<<endl; 

}

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章