繁体   English   中英

如何格式化用户输入的行?

[英]How to format a user entered line?

这是一个完全不同的问题。 另一个不是URL专用矿。 这只是一个例子。

所以这是我的代码:

main()
{
        char input[150];                                                                        //Create a variable of type char named input to store user input

        printf(" Enter a standard line: ");                                                     //Ask the user for some standard input

        if (fgets(input, 150, stdin) == NULL)                                                   //Initiate a series of checks to make sure there is valid input
        {
                puts(" End-of-File or Input Error Detected. ");                                 //If the end of the file is reached or there is a problem with input output a message
        }
        else if (input[0] == '\n')                                                              //If the user neglected to enter anything output a message
        {
                puts(" Oops! Looks like you forgot to enter something! ");
        }
        else
        {
                printf(" Here's what you entered: %s ", input);                                 //If there is valid user input echo it back to the user

            int i = 0;
            while ( input[i] != '\n' )
            {
                    if (input[i] = '/')
                            putchar("%2F");
                    i++

            }

        }
}

我必须通过将某些字符替换为其ASCII代码来替换并调整输入行。

例如:1.用户输入:google.COM/search?client
2.程序更改并以GOOGLE.com%2FSEARCH%3FCLIENT的身份打印回用户

但是,当我尝试编译我的代码时,系统给了我这条长错误消息。

/home/cs/carroll/cssc0154/One/p1.c: In function 'main':
/home/cs/carroll/cssc0154/One/p1.c:41:5: warning: passing argument 1 of 'putchar' makes integer from pointer without a cast [enabled by default]
     putchar("%2F");
     ^
In file included from /home/cs/carroll/cssc0154/One/p1.c:15:0:
/usr/include/stdio.h:580:12: note: expected 'int' but argument is of type 'char *'
 extern int putchar (int __c);

我要去哪里错了?

您试图将3个字符'%'' putchar() '传递给需要一个字符的函数putchar()

考虑使用printf("%%2f")代替putchar("%2f") ,您将在stdout中获得相同的输出。

注意:您必须使用(%)对printf中的百分比字符进行转义。

由于我没有要评论的内容...

背景:

  • C实际上并不具有“字符串”-它具有指向字符的指针,该指针与数组的处理非常相似。
  • 标准库函数期望数组中的最后一个值为'\\ 0',并且它们将继续处理字符,直到读取了这些“空字符”之一。

这行:

putchar("%2F");

将指针(char *)传递给'putchar()'而不是整数。

该警告只是指出类型不匹配,因为您不太可能这样做。 (是的,这是一个严重的错误,可能会导致崩溃或导致漏洞,具体取决于被调用函数的运行方式)

如今,大多数机器/编译器都是LP64。 这意味着'putchar()'需要一个32位的整数,并且您正在提供64位的指针。

这段代码的原因:

int i = 0;
while ( input[i] != '\n' )
{
    if (input[i] == '/')
        puts("%2F");
    i++    
}

打印出“%2F”(我假设输入为“ google.COM/search?client”)是因为它一次找到了“ /”,然后打印出您提供的字符串“%2F”。 没有其他字符执行打印语句。
如果添加:

int i = 0;
while ( input[i] != '\n' )
{
    if (input[i] == '/')
        puts("%2F");
    else
        putc(intput[i]);
    i++    
}

你会得到

google.COM%2F
search?client

因为puts()在打印输入后会打印换行符('\\ n')。

为避免这种情况,您需要选择其他输出函数,并且尽管有许多选项可用,但printf()可能是最好的,当然也是最可识别的。

printf()打印使用“%”表示格式选项的“格式化”文本,因此要打印单个“%”,则需要“转义”看起来像“ %%”的文本

这是您要尝试执行的工作示例。 我更换了,一定是一大块

if (input[i] == 'CHAR') printf("%%XX");
else if (input[i] == ...

通过将所有特殊字符放在字符串中。 我只检查该字符串是否包含我要打印的每个字符。

如果找到了字符,我将打印一个“%”,然后是字符的十六进制值。 “%02X”告诉printf()用至少2个字符写入十六进制值,如果需要填充该值,则将0放在前面。 基本上,它永远不会写“ 9”,而是写“ 09”。

您还可以使用isalpha()... isalphanum()... ispunct()函数来识别应编码的字符类型。

#include <stdio.h>
#include <string.h>

int main(int argc, char ** argv)
{
    char *decodeStr = "/:\\+";
    int   decodeLen = strlen(decodeStr);

    while(--argc >= 0 && ++argv && *argv){
        char *str = *argv;
        for(int i = 0; str[i] != '\0'; i++){
            if (NULL != memchr(decodeStr, str[i], decodeLen))
                printf("%%%02X", str[i]);
            else
                putchar(str[i]);
        }
        putchar('\n');
    }
    return 0;
}

示例运行:

./test http://google.com/atest+with+things
http%3A%2F%2Fgoogle.com%2Fatest%2Bwith%2Bthings

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM