㈠ 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]!='