I'm working on this program to take a person's name, address, and phone number and convert it URL coding print it, then unencode it then print it again normally, with labels over each part. I've run into a couple problems.
1. I think I'm overcomplicating it first of all.
2. I want to capture the input but my scanf stops capture when I type a space in my URlEncode function.
3. I haven't gotten to it yet, but I know I'm going to hit a wall when I try to convert the ASCII characters to hex and printing them to standard output (I've commented that part out)
Here is my code, I'm not looking for anyone to write my code, but I think I need a better strategy and approach. Thanks.
Code://////////////////////////////////////////////////////////////////////////////// // // File Name : URL-encoder-decoder.c // // Date : August, 2007 // // Platform : IBM PC (Windows XP) // // Compiler : Microsoft Visual C++ 6.0 // //////////////////////////////////////////////////////////////////////////////// #define _CRT_SECURE_NO_DEPRECATE #define MAX_ARRAY_SIZE 100 // maximum array size //compiler includes #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> //constants //user defined definitions typedef struct { // personal_type definition char name[MAX_ARRAY_SIZE]; char address[MAX_ARRAY_SIZE]; char phoneNumber[MAX_ARRAY_SIZE]; } PERSONAL_TYPE; //function prototypes PERSONAL_TYPE UrlEncode(void); //function to scan personal info and encode it and print it //PERSONAL_TYPE UrlDecode(void); //function to decode URL info void PrintPersonInfo (PERSONAL_TYPE personToPrint); //function to print information after decoded //////////////////////////////////////////////////////////////////////////////// // // Function Name : main // // Originated : August, 2007 // // Abstract : processing to read values for personal information and // encode then decode the information // // Parameters : None // // Return Value : error code - zero // // Misc. I/O : na // // Revisions : August, 2007 // //////////////////////////////////////////////////////////////////////////////// int main(void){ //local variables PERSONAL_TYPE personOne = {"\0", "\0", "\0"}; // empty data to pass to functions PrintPersonInfo(personOne); // function call to print personOne information by value personOne = UrlEncode(); exit(0); } //////////////////////////////////////////////////////////////////////////////// // // Function Name : PrintPersonInfo // // Originated : August, 2007 // // Abstract : function to personal name and address // // Parameters : PERSONAL_TYPE // // Return Value : none // // Misc. I/O : na // // Revisions : August, 2007 // //////////////////////////////////////////////////////////////////////////////// void PrintPersonInfo(PERSONAL_TYPE personToPrint) { printf("\n==================================================================\n"); printf("NAME: %s ADDRESS: %s PHONE NUMBER: %s\n",personToPrint.address, personToPrint.name, personToPrint.phoneNumber); // prints structures from PERSONAL_TYPE } // end function PrintPersonInfo //////////////////////////////////////////////////////////////////////////////// // // Function Name : UrlEncode // // Originated : August, 2007 // // Abstract : function to scan name, address, phone number and URL encode it // // Parameters : PERSONAL_TYPE // // Return Value : none // // Misc. I/O : na // // Revisions : August, 2007 // //////////////////////////////////////////////////////////////////////////////// PERSONAL_TYPE UrlEncode(void) { // local variables inc c; PERSONAL_TYPE inputPerson = {"<NULL>", "<NULL>", "<NULL>"}; //empty variable for function //begin printf("Enter your Name: "); // print message to user scanf("%s", inputPerson.name);// scan struture for name and assign to inputPerson printf("Enter your address: ");// print message to user scanf("%s", inputPerson.address);// scan struture for address and assign to inputPerson printf("Enter your phone number: "); // print messsage to user scanf("%s", inputPerson.phoneNumber); // scan struture for phone and assign to inputPerson /* void UrlEncode(void){ // encoding function int c=0; char *h = "0123456789abcdef"; while((c = getchar()) != EOF ){ if( 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' || c == '-' || c == '_' || c == '.' ) putchar(c); else if( c == ' ' ) putchar('+'); else { putchar('%'); putchar(h[c >> 4]); putchar(h[c & 0x0f]); } } }*/ return inputPerson; // return struture inputPerson to function caller } // end function UrlEncode ///////////////////////////////////////////////////////////////////////// /* void UrlDecode(void){ // decode function char c, c1, c2; printf("enter your sentence\n"); while( (c = getchar()) != EOF ){ if( c == '%' ){ c1 = getchar(); c2 = getchar(); if( c1 == EOF || c2 == EOF ) exit(0); c1 = tolower(c1); c2 = tolower(c2); if( ! isxdigit(c1) || ! isxdigit(c2) ) exit(0); if( c1 <= '9' ) c1 = c1 - '0'; else c1 = c1 - 'a' + 10; if( c2 <= '9' ) c2 = c2 - '0'; else c2 = c2 - 'a' + 10; putchar( 16 * c1 + c2 ); } else if( c == '+' ) putchar(' '); else putchar(c); } } */



LinkBack URL
About LinkBacks




here is the new code but I still have 1 issue. The function URLEncode spits out %20 for a space which, while is correct, should be '+' instead. I've spent a couple hours trying to figure out how to do it, but to no avail.