c语言中读入带空格的字符串

http://blog.csdn.net/pipisorry/article/details/37073023

问题:

scanf("%s", a);

运行输入hello world

回车

则输入到a的只是空格之前的部分,怎样把空格之后的部分也输出?

1. scanf"%[^\n]", str );

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

more:

scanf中的正则表达式

1、定制自己的扫描集 %[abc]、%[a-z]、%[^abc]、%[^a-z],比isdigit()、isalpha()更加灵活。[]内是匹配的字符,^表示求反集。

int i;

char str[80], str2[80];

// scanf("%d%[abc]%s", &i, str, str2);

// printf("%d %s   %s\n",i,str,str2);

// scanf("%[a-zA-Z0-9]", str);

// scanf("%[^abce]", str);

scanf("%[^a-z]", str);

printf("%s\n",str);

2、读入一个地址并显示内存地址的内容

int main(void){

char ch=‘c‘;

printf("%p\n", &ch); // print the address of ch.

char *p;

cout<<"Enter an address: ";

scanf("%p", &p);     //input the address displayed above

printf("Value at location %p is %c\n",p,*p);

return 0;

}

3、丢弃不想要的空白符:scanf("%c %c");

4、控制字符串中的非空白符:导致scanf()读入并丢弃输入流中的一个匹配字符。"%d,%d";

5、压缩输入:在格式码前加上*,则用户就可以告诉scanf()读这个域,但不把它赋予任何变量。

scanf("%c%*c, &ch); 使用此方法可以在字符处理时吃掉多余的回车。

2.scanf()

int t[999];
int sum=0;
while(scanf("%c",&t[sum++])!=EOF);

3.gets()\fgets()

#include <stdio.h>
//char *fgets( char *str, int num, FILE *stream );
int main(){
    char buffer[10];
    //fgets(buffer,10,stdin); //带有回车符
    gets(buffer);    //没有回车符

    printf("%s",buffer);
    return 0;
}

more:

gets() 不检查字符串容量,有可能越界写数据,用户可不一定给你按套路出牌,可能输入长达300字节甚至2k字节的字符串,这样的话很危险

虽然strlen,strcyp等等这些函数因为追求高效率也不会检查指针是否为空,指针是否可写,但是这些函数是C程序员来操作的,C程序员有正确使用函数的素质。

但是gets() 是用户来输入,用户并不知道字符串上限,而且就算知道,也不一定有素质去按规定使用,要多多注意

用fgets(str,80,stdin);就不危险了,fgets比较安全

from:http://blog.csdn.net/pipisorry/article/details/37073023

c语言中读入带空格的字符串

时间: 2024-08-10 19:15:35

c语言中读入带空格的字符串的相关文章

c语言中怎样用scanf()读入带空格的字符串?

楼主 发表于: 2011-01-14 15:39:55 #include <stdio.h> int main(void){ int i; char a[5]; scanf("%s", a); printf("%s\n", a); return 0; } 运行输入hello world 回车则输出的只是空格之前的部分,怎样把空格之后的部分也输出呢? 2楼 回复于: 2011-01-14 17:27:23 谁说scanf不能做到? #include <

关于“C语言中的字符数组和字符串”一些需要注意的基础点

在C语言中,没有类似Java的String类对字符串的处理,字符串的包装可以采用字符数组. 先看字符数组: #include<stdio.h> void main() { char array[] = {'a','b','c'}; int str = sizeof(array)/sizeof(char); printf("%d",str); } 此时的输出结果为:3,即字符数组的长度为3. 下面我们用字符串初始化字符数组,代码如下. #include<stdio.h&

如何在VC6.0里存储带空格的字符串

char str[20]; cin.getline(str,20) 或 string str; getline(cin,str); 据说都可以存储含空格字符串,但我用VC6.0时都出错 以下为粘贴 关于在C++中输入带空格的字符串的方法 yibcs 2012-08-10 20:44:17 此人文章 #include <iostream> #include <stdio.h>#include <string> using namespace std; void main(

C++ 输入 带空格的字符串

法一: #include <iostream> #include <string> using namespace std; void main() { char test[100]; // 定义够长的数组空间 for(int i=0;i<100;i++) test[i]='\0'; cin.getline(test,100); // 整行读取(包括空格) cout<<test<<endl; } 法二: #include <iostream>

接收一行带空格的字符串

public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while ((str = br.readLine()) != null) { List<String> resultList = executeLinuxCommand(str); for (String

C语言中通过分隔符来截取字符串

1 #include <stdio.h> 2 #include <string.h> 3 4 void reqorder(char *input,char *output,int counter) 5 { 6 int i=0, j=0; 7 char *p=input; 8 char *buf=NULL; 9 10 while((char)(*p)!='\0') 11 { 12 if((char)(*p)=='?') 13 { 14 if(i==counter) 15 { 16 b

带空格的字符串逆转(简洁版)

#include<iostream>#include<algorithm>#include<string>using namespace std; int main(){    string str;    getline(cin,str);    reverse(str.begin(),str.end());    cout<<str;    return 0;} 原文地址:https://www.cnblogs.com/zhoumin6012/p/104

C/C++ 中带空格字符串输入的一些小trick

今天在重温 C++ 的时候发现自己存在的一些问题,特此记录下来. 我们可以看一下下面这段代码: #include <iostream> #include <cstdio> #include <string> #include <cctype> using namespace std; int main(int argc, char const *argv[]) { string s; cin >> s; int cnt[26]={0};//字母统

c语言中字符串跨行书写的问题

字符串常量定义时的换行问题 如果我们在一行代码的行尾放置一个反斜杠,c语言编译器会忽略行尾的换行符,而把下一行的内容也算作是本行的内容.这里反斜杠起到了续行的作用.        如果我们不使用反斜杠,当我们试图初始化一个跨多行的字符串是,c语言编译器就会发出警告.如下面的语句所示:char letters[] = {"abcdefghijklmnopqrstuvwxyz  ABCDEFGHIJKLMNOPQRSTUVWXYZ"};      但是我们在行尾使用反斜杠, 那么就可以吧字