Removing single dot path names in URL in C - c

I'm making a function in an apache module which is supposed to fix URLs that are thrown at it. Currently I'm trying to remove single dot path names.
For example, if my URL is:
http://example.com/1/./2/./3/./4.php
Then I want the URL to be:
http://example.com/1/2/3/4.php
However I'm stuck with the logic. I'm using pointers in an effort to make this function run as fast as possible. I'm confused at the logic I should apply at the lines with //? added to the end of them.
Can someone give me advice on how to proceed? Even if its some hidden manual online? I searched bing and google for answers with no success.
static long fixurl(char *u){
char u1[10000];
char *u11=u1,*uu1=u;
long ct=0,fx=0;
while (*uu1){
*u11=*uu1;
if (*uu1=='/'){
ct++;
if (ct >=2){
uu1++;
break;
}
} else {
ct=0;
}
}
while (*uu1){
if (*uu1!='/') { //?
if (*uu1!='.') {
*u11=*uu1;
u11++;
} //?
} //?
uu1++;
}
*u11='\0';
strcpy(u,u1);
return fx;
}

You forget to look ahead one character here:
if (*uu1!='/') { //?
if (*uu1!='.') {
– you are checking the same character twice (against a 'not', so it could have some use, but your question marks indicate you are not sure what to do there and further on).
Note that you actually need to look ahead two characters. If you encounter a slash, test the next character for a . and the one after that for another /.
Rather than trying to fix your code (what is fx, the returned value, supposed to be?), I'd rewrite it from scratch to copy from source to dest and skip the offending sections. The continue makes sure that a sequence /1/././2 gets cleansed correctly to just /1/2 – it needs a chance to check the second slash again, so I just throw it back into the loop.
void fixurl (char *theUrl)
{
char *source, *dest;
source = dest = theUrl;
while (*source)
{
if (source[0] == '/' && source[1] == '.' && source[2] == '/')
{
source += 2; /* effectively, 'try again on the next slash' */
} else
{
*dest = *source;
source++;
dest++;
}
}
*dest = 0;
}
(Afterthought:)
Interestingly, adding proper support for removal of /../ is fairly trivial. If you test for that sequence, you should search backwards for the last / before it and reset dest to that position. You'll want to make sure the path is still valid, though.

This code is untested. In short, it is iterating the string (until the next character is the end sign, since if there is no next character, then you can no longer have a problem) and searches for '/'. When it finds one, analyzes the next character and handles it.
static long fixurl(char *u){
char u1[10000];
int currentIndex = 0;
if (*u == '\0') {
return 0;
}
for (; *(u + 1) != '\0'; u++){
if (*u == '/') {
if (*(u + 1) == '/') {
continue;
} else if ((*(u + 1) == '.') && (*(u + 2) == '.')) {
u++;
continue;
}
}
u1[currentIndex] = *u;
}
strcpy(u,u1);
return currentIndex;
}

here is a version of the code that works
Note it will remove all '.' that follow a '/'
However, it does not check for extraneous '/' characters being inserted into the output as the OPs posted code does not make that check.
Notice the proper formatting of the for() statement
Notice the use of meaningful names, removal of code clutter,
inclusion of a few key comments, etc
Notice the literal characters are placed on the left side of a comparison so writing a '=' when it should be '==' is caught by the compiler.
#include <string.h>
long fixurl( char * );
long fixurl(char *rawURL)
{
char cookedURL[10000] = {'\0'}; // assure new string is terminated
int currentIndex = 0;
cookedURL[currentIndex] = rawURL[0];
rawURL++;
for ( ; *rawURL; rawURL++)
{
// if prior saved char was / and current char is .
// then skip current char
if( ( '/' != cookedURL[currentIndex] )
||
( '.' != *rawURL ))
{
// copy input char to out buffer
currentIndex++;
cookedURL[currentIndex] = *rawURL;
}
} // end for
// copy modified URL back to caller's buffer
strcpy(rawURL, cookedURL);
return currentIndex+1; // number of characters in modified buffer
} // end function: fixurl

Related

The execution of the code always goes into the else statement

Some very strange things happen in my source code.
The following function works well and it prints 'y' when the password is correct and prints 'n' when it is incorrect. But if i add some UART1_Write and Delay functions to the else statement the bug comes out and even if the password is "zxc" (correct) it ALWAYS enters the else statement.
I'm using MikroC PRO for PIC v6.0.0, the robot system is made of PIC18F452 and RN-42 bluetooth module connected to it. I am testing with a laptop with a bluetooth and TeraTerm.
For more info: http://instagram.com/p/pLnU9eDL8z/#
Here it is the well working routine:
void authenticate() {
char *input = "";
char *password = "zxc\0";
unsigned char ready = 0;
while (connected && !ready) {
if (UART1_Data_Ready()) {
UART1_Read_Text(input, "|", 17);
strcat(input, "\0");
if (strcmp(input, password) == 0) {
UART1_Write('y');
ready = 1;
} else {
UART1_Write('n');
ready = 1;
}
}
}
}
This version of the routine ALWAYS goes in the ELSE statement of the strcmp(input, password) == 0 part:
void authenticate() {
char *input = "";
char *password = "zxc\0";
unsigned char ready = 0;
while (connected && !ready) {
if (UART1_Data_Ready()) {
UART1_Read_Text(input, "|", 17);
strcat(input, "\0");
if (strcmp(input, password) == 0) {
UART1_Write('y');
ready = 1;
} else {
UART1_Write('n');
Delay_ms(100);
UART1_Write('$');
Delay_ms(100);
UART1_Write('$');
Delay_ms(100);
UART1_Write('$');
Delay_ms(100);
UART1_Write('K');
Delay_ms(100);
UART1_Write(',');
Delay_ms(100);
UART1_Write('-');
Delay_ms(100);
UART1_Write('-');
Delay_ms(100);
UART1_Write('-');
Delay_ms(100);
UART1_Write('\n');
ready = 1;
}
}
}
}
It is important to send all these addition symbols in order to get RN-42 into command mode and disconnect the user if the password is wrong.
Please help me solve the problem. Any ideas appreciated!
As others have pointed out in the comments section a major issue with your code is that you are trying to store the UART data to memory that does not belong to you.
When you declare char *input = "";, you haven't actually allocated any space except for a single byte that stores '\0'. Then, when you use UART1_Read_Text(), you tell that function you may have up to 17 characters that will be read before finding the delimiter - all of which should be stored at the location pointed to by input.
The description of that library function can be found here. Also, based on the library description it looks like UART1_Read_Text() already adds the null-termination to the UART data. I base this assumption off the description of UARTx_Write_Text and the example that they provide on their website. However, I would recommend that you verify that is indeed the case.
Also, your initialization of password is redundant and char *password = "zxc\0" should be changed to char *password = "zxc". When you declare a string literal using double quotation marks it is automatically null-terminated. This excerpt is from "C in a Nutshell":
A string literal consists of a sequence of characters (and/or escape sequences) enclosed in double quotation marks... A string literal is a static array of char that contains character codes followed by a string terminator, the null character \0... The empty string "" occupies exactly one byte in memory, which holds the terminating null character.
Based on the above, I would go about it a little more like this:
#define MAX_NUM_UART_RX_CHARACTERS 17
void authenticate()
{
char input[MAX_NUM_UART_RX_CHARACTERS + 1];
char *password = "zxc";
unsigned char ready = 0;
while (connected && !ready)
{
if (UART1_Data_Ready())
{
UART1_Read_Text(input, "|", MAX_NUM_UART_RX_CHARACTERS);
if (strcmp(input, password) == 0)
{
UART1_Write('y');
ready = 1;
}
else
{
UART1_Write('n');
ready = 1;
}
}
}
}

Parsing code for GPS NMEA string

i am trying to parse the incoming GPGGA NMEA GPS string using Arduino uno and below code.
What i am trying to do is that i am using only GPGGA NMEA string to get the values of Latitude, longitude and altitude.In my below code, i had put certain checks to check if incoming string is GPGGA or not, and then store the further string in a array which can be further parsed suing strtok function and all the 3 GPS coordinates can be easily find out.
But i am unable to figure out how to store only GPGGA string and not the further string.I am using a for loop but it isn't working.
I am not trying to use any library.I had came across certain existing codes like this.
Here is the GPGGA string information link
i am trying to have following functionlity
i) Check if incoming string is GPGGA
ii) If yes, then store the following string upto EOL or upto * (followed by checksum for the array) in a array, array length is variable(i am unable to find out solution for this)
iii) Then parse the stored array(this is done, i tried this with a different array)
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10,11); // 10 RX / 11 TX
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
uint8_t x;
char gpsdata[65];
if((mySerial.available()))
{
char c = mySerial.read();
if(c == '$')
{char c1 = mySerial.read();
if(c1 == 'G')
{char c2 = mySerial.read();
if(c2 == 'P')
{char c3 = mySerial.read();
if(c3 == 'G')
{char c4 = mySerial.read();
if(c4 == 'G')
{char c5 = mySerial.read();
if(c5 == 'A')
{for(x=0;x<65;x++)
{
gpsdata[x]=mySerial.read();
while (gpsdata[x] == '\r' || gpsdata[x] == '\n')
{
break;
}
}
}
else{
Serial.println("Not a GPGGA string");
}
}
}
}
}
}
}
Serial.println(gpsdata);
}
Edit 1:
Considering Joachim Pileborg, editing the for loop in the code.
I am adding a pic to show the undefined output of the code.
Input for the code:
$GPGGA,092750.000,5321.6802,N,00630.3372,W,1,8,1.03,61.7,M,55.2,M,,*76
$GPGSA,A,3,10,07,05,02,29,04,08,13,,,,,1.72,1.03,1.38*0A
$GPGSV,3,1,11,10,63,137,17,07,61,098,15,05,59,290,20,08,54,157,30*70
$GPGSV,3,2,11,02,39,223,19,13,28,070,17,26,23,252,,04,14,186,14*79
$GPGSV,3,3,11,29,09,301,24,16,09,020,,36,,,*76
$GPRMC,092750.000,A,5321.6802,N,00630.3372,W,0.02,31.66,280511,,,A*43
$GPGGA,092751.000,5321.6802,N,00630.3371,W,1,8,1.03,61.7,M,55.3,M,,*75
$GPGSA,A,3,10,07,05,02,29,04,08,13,,,,,1.72,1.03,1.38*0A
$GPGSV,3,1,11,10,63,137,17,07,61,098,15,05,59,290,20,08,54,157,30*70
$GPGSV,3,2,11,02,39,223,16,13,28,070,17,26,23,252,,04,14,186,15*77
$GPGSV,3,3,11,29,09,301,24,16,09,020,,36,,,*76
$GPRMC,092751.000,A,5321.6802,N,00630.3371,W,0.06,31.66,280511,,,A*45
After a quick check of the linked article on the NMEA 0183 protocol, this jumped out at me:
<CR><LF> ends the message.
This means, that instead of just read indiscriminately from the serial port, you should be looking for that sequence. If found, you should terminate the string, and break out of the loop.
Also, you might want to zero-initialize the data string to begin with, to easily see if there actually is any data in it to print (using e.g. strlen).
You could use some functions from the C library libnmea. Theres functions to split a sentence into values by comma and then parse them.
Offering this as a suggestion in support of what you are doing...
Would it not be useful to replace all of the nested if()s in your loop with something like:
EDIT added global string to copy myString into once captured
char globalString[100];//declare a global sufficiently large to hold you results
void loop()
{
int chars = mySerial.available();
int i;
char *myString;
if (chars>0)
{
myString = calloc(chars+1, sizeof(char));
for(i=0;i<chars;i++)
{
myString[i] = mySerial.read();
//test for EOF
if((myString[i] == '\n') ||(myString[i] == '\r'))
{
//pick this...
myString[i]=0;//strip carriage - return line feed(or skip)
//OR pick this... (one or the other. i.e.,I do not know the requirements for your string)
if(i<chars)
{
myString[i+1] = mySerial.read() //get remaining '\r' or '\n'
myString[i+2]=0;//add null term if necessary
}
break;
}
}
if(strstr(myString, "GPGGA") == NULL)
{
Serial.println("Not a GPGGA string");
//EDIT
strcpy(globalString, "");//if failed, do not want globalString populated
}
else
{ //EDIT
strcpy(globalString, myString);
}
}
//free(myString) //somewhere when you are done with it
}
Now, the return value from mySerial.available() tells you exactly how many bytes to read, you can read the entire buffer, and test for validity all in one.
I have a project that will need to pull the same information out of the same sentence.
I got this out of a log file
import serial
import time
ser = serial.Serial(1)
ser.read(1)
read_val = ("nothing")
gpsfile="gpscord.dat"
l=0
megabuffer=''
def buffThis(s):
global megabuffer
megabuffer +=s
def buffLines():
global megabuffer
megalist=megabuffer.splitlines()
megabuffer=megalist.pop()
return megalist
def readcom():
ser.write("ati")
time.sleep(3)
read_val = ser.read(size=500)
lines=read_val.split('\n')
for l in lines:
if l.startswith("$GPGGA"):
if l[:len(l)-3].endswith("*"):
outfile=open('gps.dat','w')
outfile.write(l.rstrip())
outfile.close()
readcom()
while 1==1:
readcom()
answer=raw_input('not looping , CTRL+C to abort')
The result is this:
gps.dat
$GPGGA,225714.656,5021.0474,N,00412.4420,W,0,00,50.0,0.0,M,18.0,M,0.0,0000*5B
Using "malloc" every single time you read a string is an enormous amount of computational overhead. (And didn't see the corresponding free() function call. Without that, you never get that memory back until program termination or system runs out of memory.) Just pick the size of the longest string you will ever need, add 10 to it, and declare that your string array size. Set once and done.
There are several C functions for getting substrings out of a string, strtok() using the coma is probably the least overhead.
You are on an embedded microcontroller. Keep it small, keep overhead down. :)
#include <stdio.h>
#include <string.h>
#define GNSS_HEADER_LENGTH 5
#define GNSS_PACKET_START '$'
#define GNSS_TOKEN_SEPARATOR ','
#define bool int
#define FALSE 0
#define TRUE 1
//To trim a string contains \r\n
void str_trim(char *str){
while(*str){
if(*str == '\r' || *str == '\n'){
*str = '\0';
}
str++;
}
}
/**
* To parse GNSS data by header and the index separated by comma
*
* $GPGSV,1,1,03,23,39,328,30,18,39,008,27,15,33,035,33,1*5A
* $GNRMC,170412.000,V,,,,,,,240322,,,N,V*2D
* $GNGGA,170412.000,,,,,0,0,,,M,,M,,*57
*
* #data_ptr the pointer points to gps data
* #header the header for parsing GPGSV
* #repeat_index the header may repeat for many lines
* so the header index is for identifying repeated header
* #token_index is the index of the parsing data separated by ","
* the start is 1
* #result to store the result of the parser input
*
* #result bool - parsed successfully
**/
bool parse_gnss_token(char *data_ptr, char *header, int repeat_index, int token_index, char *result) {
bool gnss_parsed_result = FALSE; // To check GNSS data parsing is success
bool on_header = FALSE;
// For header
int header_repeat_counter = 0;
int header_char_index = 0; // each char in header index
// For counting comma
int counted_token_index = 0;
// To hold the result character index
bool data_found = FALSE;
char *result_start = result;
char header_found[10];
while (*data_ptr) {
// 1. Packet start
if (*data_ptr == GNSS_PACKET_START) {
on_header = TRUE;
header_char_index = 0; // to index each character in header
data_found = FALSE; // is data part found
data_ptr++;
}
// 2. For header parsing
if (on_header) {
if (*data_ptr == GNSS_TOKEN_SEPARATOR || header_char_index >= GNSS_HEADER_LENGTH) {
on_header = FALSE;
} else {
header_found[header_char_index] = *data_ptr;
if (header_char_index == GNSS_HEADER_LENGTH - 1) { // Now Header found
header_found[header_char_index + 1] = '\0';
on_header = FALSE;
if (!strcmp(header, header_found)) {
// Some headers may repeat - to identify it set the repeat index
if (header_repeat_counter == repeat_index) {
//printf("Header: %s\r\n", header_found );
data_found = TRUE;
}
header_repeat_counter++;
}
}
header_char_index++;
}
}
// 3. data found
if (data_found) {
// To get the index data separated by comma
if (counted_token_index == token_index && *data_ptr != GNSS_TOKEN_SEPARATOR) {
// the data to parse
*result++ = *data_ptr;
gnss_parsed_result = TRUE;
}
if (*data_ptr == GNSS_TOKEN_SEPARATOR) { // if ,
counted_token_index++; // The comma counter for index
}
// Break if the counted_token_index(token_counter) greater than token_index(search_token)
if (counted_token_index > token_index) {
break;
}
}
// Appending \0 to the end
*result = '\0';
// To trim the data if ends with \r or \n
str_trim(result_start);
// Input data
data_ptr++;
}
return gnss_parsed_result;
}
int main()
{
char res[100];
char *nem = "\
$GNRMC,080817.000,A,0852.089246,N,07636.289920,E,0.00,139.61,270322,,,A,V*04\r\n\\r\n\
$GNGGA,080817.000,0852.089246,N,07636.289920,E,1,5,1.41,11.246,M,-93.835,M,,*5E\r\n\
$GNVTG,139.61,T,,M,0.00,N,0.00,K,A*2F\r\n\
$GNGSA,A,3,30,19,17,14,13,,,,,,,,1.72,1.41,0.98,1*0A\r\n\
$GNGSA,A,3,,,,,,,,,,,,,1.72,1.41,0.98,3*02\r\n\
$GNGSA,A,3,,,,,,,,,,,,,1.72,1.41,0.98,6*07\r\n\
$GPGSV,3,1,12,06,64,177,,30,60,138,15,19,51,322,18,17,42,356,27,1*68\r\n\
$GPGSV,3,2,12,14,36,033,17,07,34,142,17,13,32,267,17,02,21,208,,1*6C\r\n\
$GPGSV,3,3,12,15,05,286,,01,05,037,,03,03,083,,20,02,208,,1*6B\r\n\
$GAGSV,1,1,00,7*73\r\n\
$GIGSV,1,1,00,1*7D\r\n\
$GNGLL,0852.089246,N,07636.289920,E,080817.000,A,A*43\r\n\
$PQTMANTENNASTATUS,1,0,1*4F\r\n";
printf("Parsing GNRMC\r\n");
printf("===============\r\n");
for(int i=1;i<=16;i++){
parse_gnss_token(nem, "GNRMC", 0, i, res);
printf("Index: %d, Result: %s\r\n", i, res);
}
printf("Parsing GNVTG (First Parameter)\r\n");
printf("================================");
// GNVTG - Header, 0 - Repeat Index(if header is repeating), 1 - Value Index,
parse_gnss_token(nem, "GNVTG", 0, 1, res);
printf("\r\nGNVTG: %s\r\n", res);
return 0;
}

checking if a string with asterisk(*) is present within another string

I am working on a program to check if a particular string is present in the given string: that is if one string is sub string of another string.
For example:
1)String: YoungPeople --> Substring to be checked: ungPeo
The output should return true.
2)String: Hello How are You? --> Substring to be checked: l*are
The output should return true.
I have used the naive based searching algorithm and it works perfectly fine for the first input.
But I am having trouble in the second kind of input where the asterisk(*) is present which should be treated as a regular expression: i.e. matches zero or more characters.
How should I check for the sub string having an * sign?
Should I try to use the same naive algorithm for searching the character before * and for the string after it? Or is there a better approach to solve this problem?
How should i check for the sub string having an * sign?
Upon reading a *, you need to try 1-2 below.
... use the same naive algorithm for searching ... is there a better approach ...?*
There are better methods. A recursive one follows.
[Edit note: 6/10 found/fixed bug]
As you progress through the string, use recursion to check the rest of the string.
The * simple allows for 2 candidate paths:
1) advance the str
2) advance the substr
Else a matching char allows advancing both.
// StarCompare() helper function
bool StarCmp(const char *str, const char *pat) {
if (*pat == '\0') return 1;
if (*pat == '*') {
if (*str) {
// advance str and use the * again
if (StarCmp(str + 1, pat)) return 1;
}
// let * match nothing and advacne to the next pattern
return StarCmp(str, pat + 1);
}
if (*pat == *str) {
return StarCmp(str + 1, pat + 1);
}
return 0;
}
bool StarCompare(const char *str, const char *pat) {
if (!str || !pat) return 0;
do {
if (StarCmp(str, pat)) return 1;
} while (*str++);
return 0;
}
[Edit Test code in previous version]
The GNU Regex Library seems like what you are looking for. If you are not familiar with regular expression, check this site.
Here is what you have to do:
Split the search string by the * character
Look for each of the parts (in the correct order) in the string you are searching
Alternatively, you can use regexes as other people have suggested.
A good place to look for a well-written implementation of glob matching would be the bash sources. But here's a simple recursive implementation that works:
#include <assert.h>
int
_glob_match(char * pattern, char * str)
{
if (!*pattern) return 1;
if (!*str) return 0;
if (*pattern == '*') return match_any_tail(pattern + 1, str);
if (*pattern != *str) return 0;
else return _glob_match(pattern + 1, str + 1);
}
int
match_any_tail(char * pattern, char * str)
{
for (; *str; str++)
if (_glob_match(pattern, str))
return 1;
return 0;
}
int glob_match(char * pattern, char * str)
{
return match_any_tail (pattern, str);
}
void
main()
{
assert(glob_match("ungPeo", "YoungPeople"));
assert(glob_match("l*are", "Hello How are You?"));
}

Remove all characters before specific character in array

Say I had the following code:
char url[63] = {'\0'};
scanf("%s", url);
And the user is asked to submit a url. I need to remove the characters that are typically at the beginnings of url such as http://, ftp://, etc...
I could check for the existence of "://" in the character array using a for loop like so:
int i;
for (i=0;i<strlen(url);i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == "/") {
// Super cool code here...
}
}
But say I wanted to delete the :// and everything that came before it? How would I accomplish that? So that if the user entered:
http://www.google.com
The output would be:
www.google.com
And a similar result if ftp:// were used.
All you need is:
int i;
for (i=0;i<strlen(url) - 2;i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == '/') {
// Super cool code here...
return &url[i+3];
}
}
To create a copy:
char* getInteresting(char* url)
{
int i = 0;
for (i=0;i<strlen(url) - 2;i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == '/') {
// Super cool code here...
int len = strlen(url) - (i+2);
char* copy = (char*)malloc(len + 1);
strcpy(copy, url + i + 3);
return copy;
}
}
}
Plus: a lot more checking through the error cases!
Usually the easiest way to do this sort of thing in C is to not actually change the original array at all, but instead to create another char * pointer to the first part of the string you actually care about. So....
int i;
char *interesting_stuff;
for (i=0;i<strlen(url);i++) {
if (url[i] == ':' && url[i+1] == '/' && url[i+2] == "/") {
interesting_stuff = url + i + 3;
}
}
and then go on to do things with interesting_stuff and leave url alone. (Be aware that it's a pointer into the same array, so if you overwrite url, you will lose interesting_stuff too.)
BTW, the bounds on that for loop will get you in trouble. You potentially look two characters past the end of the string, and my modification makes that problem a bit worse. You need to check the length of the string first and be sure that you don't go past the point in the string where :// followed by some useful data could be found. In other words, you want to constrain your loop to strlen(url) - 4 except be careful if url is shorter than 4 characters.
const char* ssc = strstr(url, "//:");
if (ssc)
{
const char* withoutProtocol = ssc + 3;
// do something with withoutProtocol
}

Another C question

I have a piece of code shown below
#include <stdio.h>
#include <stdlib.h>
void Advance_String(char [2],int );
int Atoi_val;
int Count_22;
int Is_Milestone(char [2],int P2);
char String[2] = "0";
main()
{
while(1)
{
if(Is_Milestone(String,21)==1)
{
if(atoi(String)==22)
{
Count_22 = Count_22 + 1;
}
}
Atoi_val = atoi(String);
Advance_String(S,Atoi_val);
}
}
int Is_Milestone(char P1[2],int P2)
{
int BoolInit;
char *Ptr = P1;
int value = atoi(Ptr);
BoolInit = (value > P2);
return BoolInit;
}
void Advance_String(char P1[2],int Value)
{
if(Value!=7)
{
P1[1] = P1[1]+1;
}
else
{
P1[1] = '0';
P1[0] = P1[0]+1 ;
}
}
Now my problem is Count_22 never increments as the char increments never achieves the value 21 or above.Could anyone please tell me the reason for this unexpected behaviour?My question here is to find the value of Count_22.Is there any problem with the code?
Thanks and regards,
Maddy
Your code is probably one of the worst pieces of C code i've ever seen (no offense, everybody has to learn sometime).
It has syntax errors (maybe copy/paste problem), logical problems, meaningless obfuscation, bad practices (globals), buffer overflow (atoi used on a char where there is no place to store the terminating zero byte), uninitialized values (Count_22), surprising naming convention (mixed CamelCase and underscore, variables and functions beginning with capital letter), infinite loop, no header and I forget some.
More, if you want anyone to help you debug this code, you should at list say what it is supposed to do...
To answer to the original question: why Count_22 is never incremented ?
Because Is_Milestone is always false (with or without #Jay change). Is_Milestone intend seems to be to compare the decimal value of the string "22" with the integer 21 (or 1, boolean result of 21 == 1) depending on the version).
It's logical because of Advance_String behavior. both because String has bad initial value (should probably be char String[3] = "00";) and because of the Value != 7 test. I guess what you wanted was comparing the digit with 7, but atoi works with a full string. Another minor change to achieve that Atoi_val = atoi(String+1); in the body of your loop. Then again you won't see much as the loop never stop and never print anything.
If it is a first attempt at an exercice given by some teacher (something like "programming a two digit counter in base 7" or similar). You should consider not using atoi at all and converting characters digit to value using something like:
digit_value = char_value - '0';
example:
char seven_as_char = '7';
int seven_as_int = seven_as_char - '0';
If you can explain what you are really trying to do, we may be able to show you some simple sample code, instead of the horror you are trying to debug.
EDIT
It is really more simple with original code...
After reading the Ada source, I can confirm it is indeed an Ascii based octal counter. The original code is allready of poor quality, and that explains part of the bad quality of the resulting C code.
A possible direct port could be as following (but still need a serious cleanup to look like native C code... and is quite dumb anyway as it prints a constant):
#include <stdio.h>
#include <stdlib.h>
void Advance_String(char * P1)
{
if((P1[1]-'0') != 7){
P1[1]++;
}
else{
P1[1] = '0';
P1[0]++ ;
}
}
int Is_Milestone(char * P1, int P2)
{
return (atoi(P1) > P2);
}
main()
{
int Count_11 = 0;
int Count_22 = 0;
int Count_33 = 0;
int Count_44 = 0;
char S[3] = "00";
int cont = 1;
while(cont)
{
if(Is_Milestone(S, 10)){
if(atoi(S) == 11){
Count_11 = Count_11 + 1;
}
if(Is_Milestone(S, 21)){
if(atoi(S) == 22){
Count_22 = Count_22 + 1;
}
if(Is_Milestone(S, 32)){
if(atoi(S) == 33){
Count_33 = Count_33 + 1;
}
if(Is_Milestone(S, 43)){
if(atoi(S) == 44){
Count_44 = Count_44 + 1;
}
if (atoi(S) == 77){
cont = 0;
}
}
}
}
}
Advance_String(S);
}
printf("result = %d\n", Count_11 + Count_22 + Count_33 + Count_44);
}
This statement
if(Is_Milestone(S,21==1) // Braces are not matching. If statement is not having the closing brace. Compilation error should be given.
should be
if(Is_Milestone(S,21)==1)
I guess.
Also, the code you have posted doesn't seem to be correct. It will surely give compilation errors. You have declared Count22, but are using Count_22.
Please check.

Resources