关于读取字符串和输出字符串的问题

在未知字符串长度的情况下
  1. 可以定义一个字符数组,通过scanf("%s",str),使用该方法需要添加头文件#include<stdio.h>;这种情况下碰到第一个空格就会停止读取。
    例:

    #include<stdio.h>
    int main()
    {
    	char str[40];
    	scanf("%s",str);
    	printf("%s",str);
    	return 0;	
    } 
    

    输入hello world,输出为hello

  2. 可以定义一个字符串str(在C++中),通过cin>>str,使用该方法需要添加头文件#include <iostream>并且定义命名空间using namespace std;这种情况下碰到第一个空格就会停止读取。
    例:

    #include <iostream>
    using namespace std;
    int main()
    {
    	string str;
    	cin>>str;
    	cout<<str;
    	return 0;	
    } 
    

    输入hello world,输出为hello

  3. 可以定义一个字符串str(在C++中),通过getline(cin,str)使用该方法需要添加头文件#include <iostream>并且定义命名空间using namespace std;这种情况可以读取空格符,即可以读取包含空格符的字符串。注意,如果在getline之前读取完一个整数,需要先用getchar接收整数后的换行符(因为getline函数识别换行符\n作为输入结束)
    例:

    #include<iostream>
    using namespace std;
    int main()
    {
    	string str;
    	getline(cin,str);
    	cout<<str;
    	return 0;
    }
    

    输入hello world,输出为hello world

  4. 可以定义一个字符串数组,通过gets(str)读取完整字符串;C和C++中均可以。这种情况可以读取空格符,即可以读取包含空格符的字符串。但是PAT中禁用gets函数,gets函数会删除换行符(’\n’),而scanf不会。注意,如果在gets之前读取完一个整数,需要先用getchar接收整数后的换行符(因为gets函数识别换行符\n作为输入结束)
    例:

    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
    	char str[40];
    	gets(str);
    	for(int i=0;i<strlen(str);i++)
    	cout<<str[i];
    	return 0;
    }
    

例:

	#include<stdio.h>
	#include<string.h>
	int main()
	{
		char str[40];
		gets(str);
		printf("%s",str);
		return 0;
	}

输入hello world,输出为hello world

已知字符串长度的情况下
  1. 除了以上的方法之外,还可以使用getchar函数,将字符一个一个的读取进字符数组中,这种情况下可以读取空格符
    例:

    #include<iostream>
    using namespace std;
    int main()
    {
    	char str[6];
    	for(int i=0;i<6;i++)
    		str[i]=getchar();
    	for(int i=0;i<6;i++)
    		cout<<str[i];	
    	return 0;
    }
    

    输入i hell,输出为i hell

字符串的输出问题
  1. 可以使用printf("%s",str)一次性输出整个字符串,或者通过循环遍历单次输出字符数组中的字符
    例:

    #include<stdio.h>
    int main()
    {
    	char str[40]="hello world";
    	printf("%s",str);
    	return 0;
    } 
    

例:

	#include<stdio.h>
	#include<string.h>
	int main()
	{
		char str[40]="hello world";
		for(int i=0;i<strlen(str);i++)
			printf("%c",str[i]);
		return 0;
	}

输出均为hello world

  1. 可以使用cout<<str一次性输出整个字符串(此时必须定义为字符串型而非字符数组),或者通过循环遍历单次输出字符数组中的字符。使用该方法需要添加头文件#include <iostream>并且定义命名空间using namespace std;
    例:
	#include <iostream>
	#include<cstring>
	using namespace std;
	int main()
	{
		char str[40]="hello world";
		for(int i=0;i<strlen(str);i++)
			cout<<str[i];
		return 0;
	} 

例:

	#include <iostream>
	#include<cstring>
	using namespace std;
	int main()
	{
		string str="hello world";
		cout<<str;
		return 0;
	} 

输出均为hello world

  1. 如果是用字符数组存储的字符串,可以使用puts(str)将字符数组存储的字符串输出来
    例:
	#include<stdio.h>
	#include<string.h>
	int main()
	{
		char str[40]="hello world";
		puts(str);
		return 0; 
	}

例:

	#include <iostream>
	#include<cstring>
	using namespace std;
	int main()
	{
		char str[40]="hello world";
		puts(str);	
		return 0;
	}

输出均为hello world

  1. 通过循环遍历putchar(str[i])可以将字符串或者字符数组中保存的字符一个一个输出
    例:
	#include<stdio.h>
	#include<string.h>
	int main()
	{
		char str[40]="hello world";
		for(int i=0;i<strlen(str);i++)
			putchar(str[i]);	
		return 0;
	}

例:

	#include<iostream>
	#include<cstring>
	using namespace std;
	int main()
	{
		char str[40]="hello world";
		for(int i=0;i<strlen(str);i++)
			putchar(str[i]);
		return 0;
	}

例:

	#include<iostream>
	#include<cstring>
	using namespace std;
	int main()
	{
		string str="hello world";
		for(int i=0;i<str.length();i++)
			putchar(str[i]);
		return 0;
	}

输出均为hello world

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值