C language_0321 review_string/pointer or array/string gets/other functions

I planned to do 10-0 and 10-1 today, but I found that it was a little difficult to do it. I still thought I would use 10-0 as an example and review the previous content by myself~

10-0. Talking ironically(20)

Given an English sentence, you are asked to write a program that reverses the order of all the words in the sentence and outputs it.

Input format: The test input contains a test case, giving a string with a total length of no more than 80 characters in one line. The string consists of a number of words and a number of spaces, where a word is a string of English letters (uppercase and lowercase are distinguished), and words are separated by a space. Make sure there are no extra spaces at the end of the sentence when inputting.

Output format: The output of each test case occupies one line, and the sentences in reverse order are output.

Answer 1

#include <stdio.h>//基本 输入输出的头文件
#include <string.h>// 要使用字符串函数的头文件
char str[81];//定义了一个字符串,str数组 长度为81
//在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成
int main()
{
    int i, k;
    char *p;//定义了指针p
    getchar(str);//使用字符串输入函数 读取字符到str 
//【注意:getchar的以“空格”终止】
    k = strlen(str);//计算长度 那个单词的长度
    p = str + k;//
    while (1)
    {
        if ( p == str )
        {
            printf("%s\n", p);
            break;
        }
        if (*p == ' ' && *(p+1) != ' ')
        {
            *p = '\0';
            printf("%s ", p+1);
        }
        p--;
    }
    return 0;
}

getchar error handling:

  • How to solve too many arguments to function'getchar' in devc++?

  • It says that there are too many real numeric parameters written in () when calling the getchar function. getchar has no parameters, so ( ) must be empty when calling.

gets(array) function

  • Read the string from the stdin stream until it stops when a newline character or EOF is received, and store the read result in the character array pointed to by the buffer pointer . The newline character is not used as the content of the read string. The read newline character is converted into a '\0' null character and ends the string.

Choice of getchar() and gets()

  • Read a character from standard input

  • Read a single character and process it immediately. ——getchar()

char ch;
ch=getchar();
//【“hello”---"olleh"】
  • Get the character array alone , line feed or carriage return or EOF, and then process it uniformly. --gets()

#include <stdio.h>
#include <string.h>
char str[81];
int main()
{
    gets(str);

Answer 2

#include <stdio.h>
int main(void){
    char ch[81];
    gets(ch);
    
    char *p = ch;
    char *ans = NULL;
    int flag = 1; //判断是否结束
    
    //让p指向最后
    while(*(p+1) != '\0'){
        p++;
    }
    
    while(flag){
        if(*p == ' ') //如果p所指的是空格
        {
            ans = p+1; //ans就指向p的下一个
            while(*ans != '\0' && *ans != ' '){
                printf("%c",*ans);
                ans++;
            } //输出p后面一直到空格或\0的内容
            printf(" "); //打印空格
        }
        if(p == ch) //如果p回到了第一个
        {
            flag = 0; //结束
            
            ans = p; //ans就指向p所指的位置
            while(*ans != '\0' && *ans != ' '){
                printf("%c",*ans);
                ans++;
            }
        }
        p--; //p向前移动一位
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/Shmily_as33/article/details/129681974