字符类型和字符数组

字符串的输入与输出:

对于   char s[100]

cin和scanf("%s",s);//读到空格结束,但是要注意的char s[100][100]来说,每次读入一行就是可以读入空格的直到换行

getline(cin,s)和gets(s);//只能读入一个字符串,读到换行

 

printf("%s",s);//输出不包含'\0'

puts(s);//输出并换行

 

 

字符输入和输入

getchar();

a:通常把输入的字符赋予一个字符变量,构成赋值语句例如

    char ch;

    ch=getchar();

b:getchar()函数只能接受单个字符,输入数字也按字符处理

c:输入多个字符时,只能接受第一个字符

d:getchar()函数等待用户输入,直到按回车键才结束,可用于暂停程序的运行,最后在按回车键

e:如果在程序中连续有俩个以上的getchar()函数,应该一次性输入所需字符,最后在按回车键,否则会把回车键作为一个字符     传给后面的getchar()函数

 

putchar();//输出单个字符数据

 

 

函数格式函数功能
strcat(字符串1,字符串2)将2连接到1后面,返回1的值
strncat(字符串1,字符串2,长度n)将字符串2前n个字符链接1后面,返回1
strcpy(字符串1,字符串2)

将2复制到1,返回1

strncpy(字符串1,字符串2,长度n)将1的前n个字符复制到1,返回1
strcmp(字符串1,字符串2)

1>2  返回一个正整数

1==2 返回0

1<2 返回负整数

strncmp(字符串1,字符串2,长度n)比较1和2的前n的字符,返回和上述相同
strlen(字符串名)‘\0’不计算在内
strlwr(字符串名)将字符串中大写字母转换成小写字母
strupr(字符串名)将字符串中小写字母转换成大写字母

 

 

程序举例:

输入一个字符串  然后在输入俩个字符 a和b,把a替换成b

abcde fgh kl

a b

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char s[200];
    char a,b;
    int n=0;
    while((s[n++]=getchar())!='\n');//这里的分号和getchar()加括号注意一下

    a=getchar();
    getchar();//a和b中间有空格用getchr()读入,一般换行和空格用getchar()来读
    b=getchar();
    int len=strlen(s);
    for(int i=0;i<n;i++)
    {
        if(s[i]==a)
            cout<<b;
        else
            cout<<s[i];
    }
    cout<<endl;

    return 0;
}

 

程序举例:循环输入

一个句子也许有多个连续空格,过滤掉多余的空格,只留下一个空格,头尾没有空格

hello       world. this is              c            language

 

分析:scanf()只能一个一个单词的读入,不读空格,while((scanf(“%s”,s)==1)的功能是循环读入数据,在读不到的时候停止循环

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char s[200];
    while(scanf("%s",s)==1)
        printf("%s ",s);

    return 0;
}

 

程序举例:对多个字符串排序

用选择排序

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char t[21],name[11][21];
    for(int i=0;i<10;i++)
    gets(name[i]);
    for(int i=0;i<9;i++)
    {
        int k=i;
        for(int j=i+1;j<10;j++)
        {
            if(strcmp(name[k],name[j])>0)
                k=j;
        }
        if(k>i)
        {
            strcpy(t,name[i]);
            strcpy(name[i],name[k]);
            strcpy(name[k],t);
        }

    }
    for(int i=0;i<10;i++)
        cout<<name[i]<<endl;
    return 0;
}

 

用sort排序

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;
string s[5];
int main()
{

    for(int i=0;i<5;i++)
        getline(cin,s[i]);
    sort(s,s+5);
    for(int i=0;i<5;i++)
    {
        cout<<"   "<<s[i]<<endl;
    }
    return 0;
}

 

程序举例:字符串的判等

判断俩个由大小写字母和空格组成的字符串在忽略大小写是否相等

a A bb BB ccc CCC

Aa BBbb CCCCcc

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>

using namespace std;
const int N=256;

char s1[N],s2[N];
char a[N],b[N];
int main()
{
    gets(s1);
    gets(s2);
    strlwr(s1);
    strlwr(s2);
    int a1=0;
    int b1=0;
    for(int i=0;i<strlen(s1);i++)
    {
        if(s1[i]!=' ')
            a[a1++]=s1[i];
    }
    for(int i=0;i<strlen(s2);i++)
    {
        if(s2[i]!=' ')
            b[b1++]=s2[i];
    }
    if(strcmp(a,b)==0)
        cout<<"YES"<<endl;
    else
        cout<<"NO"<<endl;

    return 0;
}

 

程序举例:字符串移位包含问题

一行,包含俩个字符串s1和s2,中间空格隔开,字符串只包含字母和数字,

如果一个字符串是另一个字符串通过若干次循环移位产生的新串的子串输出true 否则false

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int N=61;
char s1[N],s2[N];
char t[N];
char x[N];
int main()
{
    scanf("%s%s",s1,s2);
    if(strlen(s1)<strlen(s2))
    {
        strcpy(t,s1);
        strcpy(s1,s2);
        strcpy(s2,t);
    }
    strcpy(x,s1);
    if(strstr(strcat(s1,x),s2)==NULL)
        cout<<"false"<<endl;
    else
        cout<<"true"<<endl;

    return 0;
}

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值