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

时间:2017-02-22 21:37:06

标签: c

这是一个完全不同的问题。另一个是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);

我哪里错了?

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

由于我没有对上述内容发表评论......

背景:

  • 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