hexlify and unhexlify functions in C - c

Please help... I'm trying to learn C types conversion.
This code works well only converting some hex chars as 1, 2, 3. For others, also changing string or reduce string length, the conversion fails...
I'll put the code below... Anyone can explain me where the code is wrong?
Thanks in advance!
#include <stdio.h>
int a2v(char c)
{
if ((c >= '0') && (c <= '9'))
{
return c - '0';
}
if ((c >= 'a') && (c <= 'f'))
{
return c - 'a' + 10;
}
else return 0;
}
char v2a(int c)
{
const char hex[] = "0123456789abcdef";
return hex[c];
}
char *unhexlify(char *hstr)
{
char *bstr = malloc((strlen(hstr) / 2) + 1);
char *pbstr = bstr;
for (int i = 0; i < strlen(hstr); i += 2)
{
*pbstr++ = (a2v(hstr[i]) << 4) + a2v(hstr[i + 1]);
}
*pbstr++ = '\0';
return bstr;
}
char *hexlify(char *bstr)
{
char *hstr = malloc((strlen(bstr) * 2) + 1);
char *phstr = hstr;
for (int i = 0; i < strlen(bstr); i++)
{
*phstr++ = v2a((bstr[i] >> 4) & 0xFF);
*phstr++ = v2a((bstr[i]) & 0xFF);
}
*phstr++ = '\0';
return hstr;
}
int main()
{
char *title = "... Trying to convert hex into binary string and back again ?! ...";
printf("%s\n\n", title);
char *input = "0123456789abcdef\0";
printf("Original: %s (%d)\n\n", input, (int)strlen(input));
char *input_bin = unhexlify(input);
printf("Bin: %s (%d)\n\n", input_bin, (int)strlen(input_bin));
char *input_hex = hexlify(input_bin);
printf("Hex: %s (%d)\n\n", input_hex, (int)strlen(input_hex));
system("pause");
return 0;
}

Updated Version of #Kevin's answer:
int a2v(char c) {
if ((c >= '0') && (c <= '9')) return c - '0';
if ((c >= 'a') && (c <= 'f')) return c - 'a' + 10;
if ((c >= 'A') && (c <= 'F')) return c - 'A' + 10;
else return 0;
}
char v2a(int c) {
const char hex[] = "0123456789abcdef";
return hex[c];
}
char *hexlify(char *bstr) {
char *hstr=malloc((strlen(bstr)*2)+1);
bzero(hstr,(strlen(bstr)*2)+1);
char *phstr=hstr;
for(int i=0; i<strlen(bstr);i++) {
*phstr++ =v2a((bstr[i]>>4)&0x0F);
*phstr++ =v2a((bstr[i])&0x0F);
}
*phstr++ ='\0';
return hstr;
}
char *hexlifyn(char *bstr, uint str_len) {
char *hstr=malloc((str_len*2)+1);
bzero(hstr,(str_len*2)+1);
char *phstr=hstr;
for(int i=0; i<str_len;i++) {
*phstr++ =v2a((bstr[i]>>4)&0x0F);
*phstr++ =v2a((bstr[i])&0x0F);
}
*phstr++ ='\0';
return hstr;
}
char *unhexlify(char *hstr) {
char *bstr=malloc((strlen(hstr)/2)+1);
bzero(bstr,(strlen(hstr)/2)+1);
char *pbstr=bstr;
for(int i=0;i<strlen(hstr); i += 2)
*pbstr++ =(a2v(hstr[i])<<4)+a2v(hstr[i+1]);
*pbstr++ ='\0';
return bstr;
}
char *unhexlifyn(char *hstr, uint str_len) {
char *bstr=malloc((str_len/2)+1);
bzero(bstr,(str_len/2)+1);
char *pbstr=bstr;
for(int i=0;i<str_len; i += 2)
*pbstr++ =(a2v(hstr[i])<<4)+a2v(hstr[i+1]);
*pbstr++ ='\0';
return bstr;
}

Thank you!
Based on chux's answer, the mistake was into hexlify function:
*phstr++ = v2a((bstr[i] >> 4) & 0x0F);
*phstr++ = v2a((bstr[i]) & 0x0F);
Masks was wrong! I will study much more!
PS = I added some code in order to handle NULL binary character (0), that truncates string.
#include <stdio.h>
int a2v(char c)
{
if ((c >= '0') && (c <= '9'))
{
return c - '0';
}
if ((c >= 'a') && (c <= 'f'))
{
return c - 'a' + 10;
}
else return 0;
}
char v2a(int c)
{
const char hex[] = "0123456789abcdef";
return hex[c];
}
char *unhexlify(char *hstr)
{
char *bstr = malloc((strlen(hstr) / 2) + 1);
char *pbstr = bstr;
for (int i = 0; i < strlen(hstr); i += 2)
{
char c = (a2v(hstr[i]) << 4) + a2v(hstr[i + 1]);
if (c == 0) {
*pbstr++ = -128;
} else {
*pbstr++ = c;
}
}
*pbstr++ = '\0';
return bstr;
}
char *hexlify(char *bstr)
{
char *hstr = malloc((strlen(bstr) * 2) + 1);
char *phstr = hstr;
for (int i = 0; i < strlen(bstr); i++)
{
if (bstr[i] == -128)
{
*phstr++ = '0';
*phstr++ = '0';
} else {
*phstr++ = v2a((bstr[i] >> 4) & 0x0F);
*phstr++ = v2a((bstr[i]) & 0x0F);
}
}
*phstr++ = '\0';
return hstr;
}
int main()
{
printf("... Trying to convert hex into binary string and back again ?! ...\n\n");
char *input = "070790ca8fd8058b455f13dd698a4eed028cb0efc13907f681604676d69e57affe299aae7c91fd000000005271266004e4cd7f52548153f8f78b76840aeca16e91bac0ebeb4d74ff3ce3c901\0";
char *input_bin = unhexlify(input);
char *input_hex = hexlify(input_bin);
printf("Input: %s (%d)\n\n", input, strlen(input));
printf("Output: %s (%d)\n\n", input_hex, strlen(input_hex));
if (strcmp(input, input_hex) == 0)
{
printf("Input and output are EQUALS !\n\n");
} else {
printf("Input and output are NOT EQUALS !\n\n");
}
system("pause");
return 0;
}
Regards, Kevin

Related

My problem with the size of the number in the My_Mastermind minigame

can you help me with the size of the digits, for example, when I enter 01234, then everything works as it should, but it shouldn’t, the limit of digits should be within four.When I enter some four-digit number, everything works as it should work. But when some five-digit, six-digit or even more, then everything works as if it should be, but it should not work like that. And when I enter numbers that are less than four-digit, for example 123 , then it gives an error and it's good. But when I enter numbers that are more than four digits, it does not give an error and works as if it should be so.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
typedef struct s_mastermind {
int my_attempt;
char* my_code;
} my_mastermind;
my_mastermind* settings_function(my_mastermind* mastermind, int argc, char** argv);
int checking_for_correctness_num(char* _string);
int wrong_input(int progress,char* num_code);
my_mastermind* my_function();
int check_function(char* string);
char* input_function();
int mis_placed_pieces(char* bit, char* num_code);
int well_placed_pieces(char* bit, char* num_code);
int code_checker(char* bit, char* num_code);
char* size_of_function(char* strye);
char* my_strcpy(char* num1, char* num2) {
for(int i = 0; num2[i] != 0; i++) {
num1[i] = num2[i];
}
return num1;
}
int my_strlen(char* num1) {
return (*num1) ? my_strlen(++num1) + 1 : 0;
}
my_mastermind* my_function() {
my_mastermind* num = malloc(sizeof(my_mastermind));
num->my_code = malloc(5);
num->my_code[4] = '\0';
my_strcpy(num->my_code, "");
num->my_attempt = 10;
return num;
}
my_mastermind* settings_function(my_mastermind* mastermind, int argc, char** argv) {
char* bit;
for(int i = 0; i < argc;) {
if (my_strlen(argv[i]) == 2 && argv[i][0] == '-') {
if(argv[i][1] == 'c') {
char* num_code = argv[i + 1];
if(wrong_input(argc,num_code) != 0) {
break;
}
my_strcpy(mastermind->my_code, num_code);
}else if(argv[i][1] == 't') {
if(checking_for_correctness_num(argv[i + 1]) == 0) {
mastermind->my_attempt = check_function(argv[i + 1]);
}
} else {
printf("WRONG FLAG RESTART THE GAME!!!\n");
}
}
i += 1;
}
return mastermind;
}
int wrong_input(int progress,char* num_code) {
// if(my_strlen(num_code) != 4) {
// printf("Code bigger than 4\n");
// }
if(checking_for_correctness_num(num_code) == 1) {
printf("Wrong input!\n> ");
fflush(stdout);
char* code = input_function();
char* variable = size_of_function(code);
free(code);
int results = 1;
if(wrong_input(progress,variable) == 0) {
results = code_checker(num_code, variable);
}
free(variable);
return results;
}
return 0;
}
int checking_for_correctness_num(char* _string) {
for(int i = 0; _string[i] != '\0'; i++) {
if(!(_string[i] >= '0' && _string[i] <= '9')) {
return 1;
}
}
return 0;
}
int check_function(char* string) {
int check_num = 0;
for(int i = 0; string[i] != '\0'; i++) {
check_num = check_num * 10 + (string[i] - '0');
}
return check_num;
}
char* input_function() {
char* getting = malloc(101);
getting[100] = '\0';
read(0, getting, 100);
fflush(stdout);
return getting;
}
int game_progress(int progress, char* bit) {
printf("Round: %d\n> ", progress);
fflush(stdout);
char* code = input_function();
char* variable = size_of_function(code);
free(code);
int results = 1;
if(wrong_input(progress,variable) == 0) {
results = code_checker(bit, variable);
}
free(variable);
return results;
}
void game_action(my_mastermind* mastermind) {
int current_try = 0;
for (;current_try < mastermind->my_attempt;) {
int results = game_progress(current_try, mastermind->my_code);
current_try += 1;
if(results == 0) {
printf("Congratz! You did it!\n");
break;
}
}
}
int code_checker(char* bit, char* num_code) {
int good_w = well_placed_pieces(bit, num_code);
int not_good_m = mis_placed_pieces(bit, num_code);
if(good_w == 3 || good_w == 2 || good_w == 1 || not_good_m == 3 || not_good_m == 2 || not_good_m == 1){
printf("Well placed pieces: %d\nMisplaced pieces: %d\n---\n", good_w,not_good_m);
}
if(good_w == 4) {
return 0;
} else {
return 1;
}
}
int well_placed_pieces(char* bit, char* num_code) {
int number = 0;
for(int i = 0; i < 4; i++) {
if (bit[i] == num_code[i]) {
number += 1;
}
}
return number;
}
int mis_placed_pieces(char* bit, char* num_code) {
int number = 0;
int i = 0;
int j = 0;
while(i < 4) {
i++;
if (bit[i] == num_code[i]) {
number += 1;
}
}
return number;
}
char* size_of_function(char* strye) {
char* new_string = malloc(5);
new_string[4] = '\0';
for(int i = 0; i < 4;i++){
new_string[i] = strye[i];
}
return new_string;
}
int main(int argc, char** argv) {
printf("Will you find the secret code?\n---\n");
my_mastermind* mastermind = my_function();
settings_function(mastermind, argc, argv);
game_action(mastermind);
free(mastermind);
return 0;
}
The problem is that you size_of_function assumes the input string is exactly 4 character long, not counting the '\0'. You should either check if the input string and return a error via a NULL pointer, or fully copy the string and check later.
Returning a NULL pointer require the least modification. You can do it by checking the input string size first :
char* size_of_function(char* strye) {
if(my_strlen(strye) != 4)
return NULL;
char* new_string = malloc(5);
new_string[4] = '\0';
for(int i = 0; i < 4;i++){
new_string[i] = strye[i];
}
if (strye[4] == '\r' || strye[4] == '\n' || strye[4] == '\0')
return new_string;
free(new_string);
return NULL;
}
Then, in wrong_input(), check if num_code is NULL :
int wrong_input(int progress,char* num_code) {
if(num_code == NULL || checking_for_correctness_num(num_code) == 1) {
printf("Wrong input!\n> ");
fflush(stdout);
char* code = input_function();
char* variable = size_of_function(code);
free(code);
int results = 1;
if(wrong_input(progress,variable) == 0) {
results = code_checker(num_code, variable);
}
free(variable);
return results;
}
return 0;
}
It is critical to check if num_code is NULL before calling checking_for_correctness_num(). In C the || operator evaluates the left operand first and skip the second operand evaluation if the first one is true. This way we can ensure that we never pass a NULL pointer to checking_for_correctness_num().
wrong_input() is called recursively and allocates memory without freeing it before calling itself. This can eat up memory fast and is generality considered to be bad practice.
Also, you've implemented my_strlen() as a recursive function, which isn't necessary. Using a loop is better :
int my_strlen(char* num1) {
int index = 0;
while(num1[index++]); //Note that 'index' is post-incremented
return index - 1; //Subtract one to account for the last post increment
}

Find index of word in string

I want to write a function which will find index of word in string.
For example if string is
This is word.
my function for string "word" should return number 3.
Note: functions from string.h library and auxiliary strings are not allowed.
How could I do this in C?
I can't think of a solution better than this (though there might be better ones).
#include <stdio.h>
int main() {
char word[] = "This is a word";
int flag = 0, space = 0, pos = -1;
for (int i = 0; word[i] != '\0'; i++) {
if (flag == 1) {
break;
}
for (int j = 0; word[j] != '\0'; j++) {
if (flag == 1) {
break;
}
else if (word[j+1] == '\0' || word[j+2] == '\0' || word[j+3] == '\0') {
break;
}
else {
if (word[j] == 'w' && word[j+1] == 'o' && word[j+2] == 'r' && word[j+3] == 'd') {
flag = 1;
pos = j;
}
}
}
}
for (int i = 0; word[i] != '\0'; i++) {
if (word[i] == ' ' || word[i] == '!' || word[i] == '#') {// And many more symbols
fchars++;
}
else {
break;
}
}
if (flag == 1 && pos-1 > 0 && word[pos-1] == ' ') {
for (int i = 0; i < pos; i++) {
if (word[i] == ' ') {
space++;
}
}
printf("Found at position = %i\n", space+1-fchars);
}
else {
printf("Not found!\n");
}
}
You can split the sentence by space to get the words and then match each word in the sentence with the word you want to match
Please check this modified code:
#include<stdio.h>
int main()
{
char word[] = "word";
char string[100];
gets(string);
int curWordStart = -1;
int curWordEnd = -1;
int wordCount = 0;
int i = 0;
for (i = 0; string[i] != '\0'; i++)
{
if (string[i] == ' ')
{
int curWordLength = curWordEnd - curWordStart + 1;
if (curWordStart != -1 && curWordLength > 0)
{
wordCount++;
int foundMatch = 1;
int j;
int k = 0;
for (j = curWordStart; j <= curWordEnd; j++) {
if (word[k] == '\0') {
foundMatch = 0;
break;
}
if (word[k] != string[j])
{
foundMatch = 0;
break;
}
k++;
}
if (word[k] != '\0')
{
foundMatch = 0;
}
if (foundMatch == 1)
{
printf("%d\n", wordCount);
}
}
curWordStart = -1;
curWordEnd = -1;
}
else if ((string[i] >= 'a' && string[i] <= 'z') || (string[i] >= 'A' && string[i] <= 'Z'))
{
if (curWordStart == -1) {
curWordStart = i;
}
curWordEnd = i;
}
}
int curWordLength = curWordEnd - curWordStart + 1;
if (curWordStart != -1 && curWordLength > 0)
{
wordCount++;
int foundMatch = 1;
int j;
int k = 0;
for (j = curWordStart; j <= curWordEnd; j++) {
if (word[k] == '\0') {
foundMatch = 0;
break;
}
if (word[k] != string[j])
{
foundMatch = 0;
break;
}
k++;
}
if (word[k] != '\0')
{
foundMatch = 0;
}
if (foundMatch == 1)
{
printf("%d\n", wordCount);
}
}
return 0;
}
It will print each position of the searched word in the sentence. If you want to just print the first one, you can easily modify it.
Here are steps to follow:
you must specify precisely what is a word in the string.
measure the length len of the word to search
define an int index = 1
in a loop, using a pointer p starting at the beginning of the string:
advance p past all word delimiters (spaces, punctuation or non letters?)
if p is at end of string return 0 (not found).
measure the length len1 of the current word in the string
if len1 == len and all bytes are identical to those of the word, return index
otherwise skip the word by advancing p by len1, increment index and continue the loop.
Here is an implementation:
#include <stddef.h>
int isletter(char c) {
/* assuming ASCII */
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
}
int word_index(const char *str, const char *word) {
const char *p = str;
size_t len, len1, i;
int index = 1;
for (len = 0; word[len]; len++)
continue;
for (;;) {
while (!is_letter(*p))
p++;
if (*p == '\0')
return 0;
for (len1 = 0; is_letter(p[len1]); len1++)
continue;
if (len1 == len) {
for (i = 0; i < len && p[i] == word[i]; i++)
continue;
if (i == len)
return index;
}
p += len1;
index++;
}
}

Why does 'a' prematurely end this loop?

Pointers rather than straight answers if you will please.
This loop does some manipulation on chars and outputs a ciphertext c based on a key k and some plaintext p.
When 'a' or 'A' comes up in the plaintext, the program will output that letter as expected but then end the loop prematurely.
p suddenly becomes just one character long, this character being 1.
while (i < strlen(p))
{
char stdp = p[i];
char stdk = k[j];
if (isalpha(stdp))
{
if (islower(stdp))
{
p[i] = stdp - 'a';
Aa = 0;
}
else
{
p[i] = stdp - 'A';
Aa = 1;
}
if (islower(k[j]))
{
k[j] = stdk - 'a';
}
else
{
k[j] = stdk - 'A';
}
}
if (isalpha(stdp))
{
c[i] = ((p[i] + k[j]) % 26);
}
else
{
c[i] = p[i];
}
if (isalpha(stdp))
{
if (Aa == 1)
{
c[i] = c[i] + 'A';
}
else if (Aa == 0)
{
c[i] = c[i] + 'a';
}
}
if (isalpha(stdp))
{
if (j + 1 == kk)
{
j = (j + 1) % kk;
strcpy(k, argv[1]);
}
else
{
j++;
}
}
i++;
}
I have now solved the issues I was facing with the program terminating early.
Most of the issues seem to have been solved by stopping the constant refs to strlen(p) and just assigning that to a static variable. I have tried to tidy up the code as suggested.
int main(int argc, char *argv[])
{
if (argc != 2 || argv[1] == NULL)//no more than 1 arg and not empty
{
printf("Command requires one argument to run.\n");
return 1;
}
char *k = malloc(100);
strcpy(k, argv[1]);
int kInt = strlen(k);
for (int i = 0, n = kInt; i < n ; i++)// iterating though the key to check it is alphabetic
{
if (isalpha(k[i]) == false)
{
printf("Make sure key is alphabetic!\n");
return 1;
}
}
printf ("plaintext: ");
char *p = malloc(100);
strcpy(p, get_string());
int pInt = strlen(p);
int i = 0;
int j = 0;
int Aa = 0;
char *c = malloc(100);
while (j < kInt)
{
if (islower(k[j]))
{
k[j] = k[j] - 'a';
}
else
{
k[j] = k[j] - 'A';
}
j++;
}
j = 0;
while (i < pInt)
{
if (isalpha(p[i]))
{
if (islower(p[i]))
{
p[i] = p[i] - 'a';
Aa = 0;
}
else
{
p[i] = p[i] - 'A';
Aa = 1;
}
c[i] = ((p[i] + k[j]) % 26);
if (Aa == 1)
{
c[i] = c[i] + 'A';
}
else if (Aa == 0)
{
c[i] = c[i] + 'a';
}
if (j + 1 == kInt)
{
j = (j+1) % kInt;
}
else
{
j++;
}
}
else if (isalpha(p[i]) != true)
{
c[i] = p[i];
}
i++;
}
printf("ciphertext: %s\n", c);
free(c);
free(k);
free(p);
}

word frequency of string counter is sometimes wrong

I hope you can help me I worked on this code. The code works like this
user inputs a string for example "hey john, how are you john?
the program erases signs like "'?' , ',' '!' " etc.
the program writes a string after erasing the signs : "hey john how are you john?"
and the code outputs the frequency of each word:
hey : 1
john: 2
how : 1
are : 1
you : 1
but my code counts sometimes wrong. For example when I type "bye bye bye hello hello hello"
the output is :
bye : 3
hello : 1
My code does the john example right, but the bye bye... example wrong.
How do I have to change my code? Thank you
#include <stdio.h>
#include <string.h>
char words[80][80];
void clear_string(char *text);
int extract_and_count(char *source, int *count);
void clearArray(char array[]);
int indexInWords(char string[]);
void print(int countOfWords, int count[]);
int equals(char *string1, char *string2);
int main() {
char string[80];
int count[80];
printf("please enter your text: ");
scanf("%[^\n]s", string);
clear_string(string);
printf("%s\n", string);
int countOfWords = extract_and_count(string, count);
print(countOfWords, count);
return 0;
}
void clear_string(char *text){
int i = 0;
for(;i < strlen(text);++i){
if( text[i] == '.' || text[i] == ',' || text[i] == '!' || text[i] == '?'){
int k = i + 1;
for(; k < strlen(text);++k){
text[k-1] = text[k];
}
k = strlen(text) - 1;
text[k] = ' ';
}
}
}
int extract_and_count(char *source, int *count){
int wordCounter = 0;
char string[80];
int i = 0, k = 0;
clearArray(string);
for(; i < strlen(source);++i, ++k){
if(source[i] != ' '){
string[k] = source[i];
}else{
if(string[0] == '\0'){
break;
}
int index = indexInWords(string);
if(index == -1){
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
}else{
count[index] += 1;
}
clearArray(string);
k = -1;
}
}
return wordCounter;
}
void clearArray(char array[]){
memset(array,0,strlen(array));
//array[0] = '\0';
}
int indexInWords(char string[]){
int i = 0;
for(;i < 80;++i){
if(equals(words[i], string) == 0){
return i;
}
}
return -1;
}
void print(int countOfWords, int count[]){
for(int i = 0;i < countOfWords; ++i){
printf("%s : %d\n",words[i], count[i]);
}
}
int equals(char string1[], char string2[]){
return strcmp(string1, string2);
}
The most significant problem I found was in extract_and_count() -- it doesn't count the last word as it only counts words followed by space. The bandaid is to check if string has anything in it after the loop, and if so, process it. Below is my rework for that fix and general style:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
void clear_string(char *text);
int extract_and_count(char *source, int count[]);
void clearArray(char array[]);
int indexInWords(char string[]);
void print(int countOfWords, int count[]);
bool equals(char *string1, char *string2);
#define BUFFER_SIZE (512)
#define MAX_WORD_COUNT (80)
#define MAX_WORD_SIZE (64)
char words[MAX_WORD_COUNT][MAX_WORD_SIZE];
int main() {
char string[BUFFER_SIZE];
int count[MAX_WORD_COUNT];
printf("Please enter your text: ");
while (fgets(string, BUFFER_SIZE, stdin) == NULL) {
printf("Please (re)enter your text: ");
}
clear_string(string);
int countOfWords = extract_and_count(string, count);
print(countOfWords, count);
return 0;
}
void clear_string(char *text) {
for (int i = 0; i < strlen(text); i++) {
if (text[i] == '.' || text[i] == ',' || text[i] == '!' || text[i] == '?' || text[i] == '\n') {
int length = strlen(text);
for (int k = i + 1; k < length; k++) {
text[k - 1] = text[k];
}
text[length - 1] = '\0';
i--;
}
}
}
int extract_and_count(char *source, int count[]) {
int wordCounter = 0;
char string[MAX_WORD_SIZE] = {'\0'};
for (int i = 0, k = 0; i < strlen(source); i++, k++) {
if (source[i] != ' ') {
string[k] = source[i];
} else {
if (string[0] == '\0') {
break;
}
int index = indexInWords(string);
if (index == -1) {
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
} else {
count[index] += 1;
}
clearArray(string);
k = -1;
}
}
if (string[0] != '\0') {
int index = indexInWords(string);
if (index == -1) {
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
} else {
count[index] += 1;
}
}
return wordCounter;
}
void clearArray(char array[]) {
memset(array, 0, strlen(array));
}
int indexInWords(char string[]) {
for (int i = 0; i < MAX_WORD_COUNT; i++) {
if (equals(words[i], string)) {
return i;
}
}
return -1;
}
void print(int countOfWords, int count[]) {
for (int i = 0; i < countOfWords; i++) {
printf("%s : %d\n", words[i], count[i]);
}
}
bool equals(char string1[], char string2[]) {
return strcmp(string1, string2) == 0;
}
The next most significant issue I see is you don't keep track of how many entries in words[][] are used, so indexInWords() could easily wander off making comparisons against uninitialized memory.
In extract_and_count you break out of the for-loop when you find 2 spaces. Also you did not check for the last word of source. Changed it to:
int extract_and_count(char *source, int *count){
int wordCounter = 0;
char string[80];
int i = 0, k = 0;
clearArray(string);
for(; i < strlen(source)+1;++i, ++k){
if(source[i] != ' ' && source[i] != 0){
string[k] = source[i];
}else{
if(string[0] != '\0'){
int index = indexInWords(string);
if(index == -1){
strcpy(words[wordCounter], string);
count[wordCounter] = 1;
wordCounter++;
}else{
count[index] += 1;
} }
clearArray(string);
k = -1;
}
}
return wordCounter;
}

Converting a string of digits to an integer, storing the result

Sorry for the confusing question, but what I'm trying to do is store an array in a variable.
I want to store the numbers in *value so that instead of int value: -12118433669 it will be int value: 123456789.
OUTPUT
123456789
array: '123456789' int value: -1218433669
0001234
array: '0001234' int value: -1218433669
5
array: 'abc5xyz' int value: -1218433669
array: '' int value: -1218433669
987654321
array: '987654321' int value: -1218433669
SOURCE
#include <stdio.h>
MyFNatoi(char *numArray, int *value) {
int i;
for (i = 0; i < 10; i++) {
if (numArray[i] > 47 && numArray[i] < 58) {
printf("%c", numArray[i] - 0);
}
}
}
int main() {
char numbers[5][10] = { "123456789", "0001234", "abc5xyz", "", "987654321" };
int i, value;
for(i = 0; i < 5; i++) {
MyFNatoi(numbers[i], &value);
printf("\narray: '%s' int value: %d\n", numbers[i], value);
}
return 0;
}
I propose this function:
void MyFNatoi(const char *str, int *value)
{
if (value == NULL)
return;
*value = 0;
if (str != NULL)
{
int negative = 0;
if (*str == '-')
{
negative = 1;
str++;
}
while (*str && isdigit(*str))
{
*value = (*value * 10) + (*str++ - '0');
}
if (negative)
*value *= -1;
}
}
It handles negative numbers, and only checks leading digits (so wont make a number out of "abc123def456" for example).
#define Zero '0'
#define Nine '9'
void MyFNatoi(char *numArray, int *value) {
int i;
*value = 0;
for (i = 0; i < 10 && numArray[i] != 0; i++) {
if (numArray[i] >= Zero && numArray[i] <= Nine) {
*value = *value * 10 + (numArray[i] - Zero);
}
}
}
My Version:
void MyFNatoi(char *numArray, int *value)
{
for (*value = 0; *numArray != '\0'; ++numArray) {
if (0x30 <= *numArray && *numArray <= 0x39) {
*value = 10* *value + *numArray - 0x30;
}
}
}

Resources