㈠ C++從鍵盤上讀取字元串存入數組中
用stl的vector唄,可以動態擴展大小,用法與數據很相似。
大小不夠的時候resize一下就行了。
結果在words變數中。
#include<iostream>
#include<string>
#include<vector>
usingnamespacestd;
intmain()
{
stringinputStr;
getline(cin,inputStr);
vector<string>words;
intpos=0,lastPos=0;
while((pos=inputStr.find('',lastPos))!=string::npos)
{
words.push_back(inputStr.substr(lastPos,pos-lastPos));
lastPos=pos+1;
}
if(lastPos<inputStr.size())
{
words.push_back(inputStr.substr(lastPos));
}
system("pause");
return0;
}
㈡ 從鍵盤輸入一個字元串,存入一個數組中,求輸入的字元串的長度.
你的 int func(int string[]是函數的聲明,不是字元串的聲明;還有 不能聲明字元串為string,因為string是c語言已經定義的關鍵字,其實就是頭文件了,可以這樣寫:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
int n;
gets(str);
n=strlen(str);
printf("n==%d\n",n);
}
㈢ 編寫從鍵盤輸入一個字元串存入數組a中,利用指針輸出字元串中前5個字元的程序
編寫從鍵盤輸入一個字元串存入數組a中,利用指針輸出字元串中前5個字元的程序:
import java.util.Scanner
public class a
public static void main(String[] args)
Scanner sc=new Scanner(System.in)
String[] str= new String[5]
System.out.println("請從控制台輸入5個字元串,按回車確認!")
for(int i=0;i<5;i++)
str[i]=sc.nextLine()
for(int j=4;j>=0;j--)
System.out.println(str[j])
通常以串的整體作為操作對象
如:在串中查找某個子串、求取一個子串、在串的某個位置上插入一個子串以及刪除一個子串等。兩個字元串相等的充要條件是:長度相等,並且各個對應位置上的字元都相等。設p、q是兩個串,求q在p中首次出現的位置的運算叫做模式匹配。串的兩種最基本的存儲方式是順序存儲方式和鏈接存儲方式。
㈣ 從鍵盤輸入一個字元串存入字元數組,統計數字,字母,空格和其他字元的個數.
#include<stdio.h>
intmain()
{
charstr[100];
inti=0;
intnum=0,ch=0,blank=0,other=0;
gets(str);
while(str[i]!='