[英]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中的百分比字符進行轉義。
由於我沒有要評論的內容...
背景:
這行:
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.