11.3字符串输出

11.3字符串输出

put(),fputs(),printf()用于打印输出字符串

11.3.1puts函数

puts函数只需要把字符串的地址作为参数传递给他就行

#include <stdio.h> 
#define DEF "I am a #defined string." 
int main(void)
{
char str1[80] = "An array was initialized to me.";
const char * str2 = "A pointer was initialized to me.";
puts("I'm an argument to puts().");
puts(DEF);
puts(str1);
puts(str2);
puts(&str1[5]);//从数组第五个字符开始打印
puts(str2 + 4);//从指针第四个字符开始
return 0;
}

在第5个 puts()调用中,表达式&str1[5]是str1数组的第6个元素(r),puts()从该元素 开始输出。与此类似,第6个puts()调用中,str2+4指向储存"pointer"中i的存 储单元,puts()从这里开始输出。

puts函数在遇到空字符时1就会停止

11.3.2fputs函数

fputs函数是puts针对文件定制的版本

fputs函数的第二个参数指明要写入数据的文件,如果要打印在显示器上,可以用定义在stdio.h的stdout(标准输出)作为参数

fputs不会在输出的末尾添加换行符

11.3.3printf()函数

和puts一样,printf也把字符串的地址作为参数,不过也不会添加换行符

include <ctype.h>

define LEN 10

char* getword(char* str);

int main(int argc, char* argv[])
{
char input[LEN];

printf("Please enter a word (EOF to quit):\n");
while (getword(input) != NULL)
{
    printf("Result:\n");
    puts(input);
    printf("You can enter a word again (EOF to quit):\n");
}
printf("Done.\n");

return 0;

}

char* getword(char* str)
{
int ch;
int n = 0;
char* pt = str;

while ((ch = getchar()) != EOF && isspace(ch))
    continue;
//↑跳过第一个非空白字符前面的所有空白符;

if (ch == EOF)
{
    return NULL;
    //↑若第一次直接输入Ctrl+Z(Windows)或Ctrl+D(Unix/Linux/Mac os)则返回空指针;
}
else
{
    n++;
    *str++ = ch;
    /*↑把第一个非空白字符赋值给str
    所指向的内存空间内并指向下一个
    存储空间;*/
}
while ((ch = getchar()) != EOF && !isspace(ch) && (n < LEN - 1))
{
    *str++ = ch;
    /*↑从第2个字符开始赋值直到
    遇见单词后面第一个空白符;*/
    n++;
}
*str = '\0';

if (ch == EOF)
{
    return NULL;

}
else
{
    while (getchar() != '\n')
        continue;
    //↑从单词后面丢弃输入行中的其它字符;
    return pt;
}

}

include <ctype.h>

define LEN 10

char *getword(char *str, int len);

int main(int argc, char *argv[])
{
char input[LEN];

printf("Please enter a word (EOF to quit):\n");
while (getword(input, LEN - 1) != NULL)
{
    printf("Result:\n");
    puts(input);
    printf("You can enter a word again (EOF to quit):\n");
}
printf("Done.\n");

return 0;

}

char *getword(char *str, int len)
{
int ch;
int n = 0;
char *pt = str;

while ((ch = getchar()) != EOF && isspace(ch))
    continue;
//↑跳过第一个非空白字符前面的所有空白符;

if (ch == EOF)
{
    return NULL;
    //↑若第一次直接输入Ctrl+Z(Windows)或Ctrl+D(Unix/Linux/Mac os)则返回空指针;
}
else
{
    n++;
    *str++ = ch;
    /*↑把第一个非空白字符赋值给str
    所指向的内存空间内并指向下一个
    存储空间;*/
}
while ((ch = getchar()) != EOF && !isspace(ch) && n < len)
{
    *str++ = ch;
    /*↑从第2个字符开始赋值直到
    遇见单词后面第一个空白符;*/
    n++;
}
*str = '\0';

if (ch == EOF)
{
    return NULL;
    /*↑输入Ctrl+Z(Windows)或Ctrl+D(Unix/Linux/Mac os)返回空指针;*/
}
else
{
    while (getchar() != '\n')
        continue;
    //↑从单词后面丢弃输入行中的其它字符;
    return pt;
}

}

本文作者:New灬撕裂の天堂

本文链接:https://www.cnblogs.com/newbroken/p/15875563.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

本文作者:New灬撕裂の天堂

本文链接:https://www.cnblogs.com/newbroken/p/15875563.html

posted @   New灬撕裂の天堂  阅读(47)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.