I am trying to make an encrypter, but as you can see, it's not working.
str[] have an undeclared array size, i know that theres an option to make it with a pointer array, but i really
want to make it a fixed size, also, so i can skip the "int strSize = sizeof(str);" in every function.

Hope that i makes sense and the code is not to messi.

// ENCRYPTING STRINGS
// 06-03-2012
// By Morten Lund
#include <stdio.h>
#include <stdlib.h>

void printstr(char str[]);
void crypt(char str[]);
void decrypt(char str[]);

#define ALPHA "abcdefghijklmnopqrstuvwxyz"
#define CRYPT_ALPHA "zyxwvutsrqponmlkjihgfedcba"
#define ALPHA_SIZE sizeof(ALPHA)
#define CRYPT_ALPHA_SIZE sizeof(CRYPT_ALPHA)

int main(int argc, char *argv[]) {

	
	char str[] = "someword";

	printf("Original string:  ");
	printstr(str);
	
	crypt(str);
	printf("\nCrypted string:   ");
	printstr(str);
	
	decrypt(str);
	printf("\nDecrypted string: ");
	printstr(str);
	printf("\n");

	return EXIT_SUCCESS;
}

void printstr(char str[]) {
	int i; int strSize = sizeof(str);
	
	for (i = 0; i < strSize; i++) {
		printf("%c", str[i]);
	}
}

void crypt(char str[]) {
	int i; int k; int strSize = sizeof(str);
	
	for (i = 0; i < strSize; i++) {
		for (k = 0; k < ALPHA_SIZE; k++) {
		
			if (str[i] == ALPHA[k]) {
				str[i] = CRYPT_ALPHA[k];
            }
		}
	}
}

void decrypt(char str[]) {
	int i; int k; int strSize = sizeof(str);
     
     for (i = 0; i < strSize; i++) {
         for (k = 0; k < CRYPT_ALPHA_SIZE; k++) {
             
             if (str[i] == CRYPT_ALPHA[k]) {
                 str[i] = ALPHA[k];                             
             }      
             
             
         }    
     }
     
     
     
     
}

>>so i can skip the "int strSize = sizeof(str);" in every function.

Just as well because sizeof does not return the length of the buffer, only the size of a pointer (which on 32-bit compilers is 4). What you will probably have to do is pass the size of the array as another argument to the functions, such as void printstr(char str[], int size);

Edited 4 Years Ago by Ancient Dragon: n/a

Funny thing. I had a function to count the array size, but deleted it when i found sizeof. Guess i just assumed it to work different, didn't read about it (going to now).

MonsieurP > When you say "ptr" are you referring to a pointer to a char array? How would i define the size when the array is declared inside main?

Now looks like this:

// ENCRYPTING STRINGS
// 06-03-2012
// By Morten Lund
#include <stdio.h>
#include <stdlib.h>

#define ALPHA "abcdefghijklmnopqrstuvwxyz"
#define CRYPT_ALPHA "zyxwvutsrqponmlkjihgfedcba"
#define ALPHA_SIZE 25
#define CRYPT_ALPHA_SIZE 25

int  arraysize(char str[]);
void printstr(char str[], int strSize);
void crypt(char str[], int strSize);
void decrypt(char str[], int strSize);

int main(int argc, char *argv[]) {

	char str[] = "someword";
	int strSize = arraysize(str);

	printf("Original string:  ");
	printstr(str, strSize);
	
	crypt(str, strSize);
	printf("\nCrypted string:   ");
	printstr(str, strSize);
	
	decrypt(str, strSize);
	printf("\nDecrypted string: ");
	printstr(str, strSize);
	printf("\n");

	return EXIT_SUCCESS;
}


int  arraysize(char str[]) {
     int size = 0;
     
     while (str[size] != 0) {
           size++;      
     }     
     return size;
}

void printstr(char str[], int strSize) {
	int i;
	
	for (i = 0; i < strSize; i++) {
		printf("%c", str[i]);
	}
}

void crypt(char str[], int strSize) {
	int i; int k;
	
	for (i = 0; i < strSize; i++) {
		for (k = 0; k < ALPHA_SIZE; k++) {
		
			if (str[i] == ALPHA[k]) {
				str[i] = CRYPT_ALPHA[k];
            }
		}
	}
}

void decrypt(char str[], int strSize) {
	int i; int k;
     
     for (i = 0; i < strSize; i++) {
         for (k = 0; k < CRYPT_ALPHA_SIZE; k++) {
             
             if (str[i] == CRYPT_ALPHA[k]) {
                 str[i] = ALPHA[k];                             
             }      
             
             
         }    
     }
     
     
     
     
}

Buut still not working. I have no idia, but I'm pretty sure that it has something to do with the str[] not being declared with a size. But my output still look weird, when it goes over the letter 'm' it starts over :s

This macro

#define ARRAY_SIZE(ptr) (sizeof(ptr)/sizeof(ptr[0]))

will return the number of elements in a 1D array.

No it won't. When ptr is char* then that macro will always return 4 because sizeof(ptr) == 4 and sizeof(ptr[0]) == 1 regardless of the number of bytes allocated to the character array.

That macro only works in the function in which the character array (or any other king of array) was originally declared.

Edited 4 Years Ago by Ancient Dragon: n/a

Now looks like this:

int main(int argc, char *argv[]) {

	char str[] = "someword";
	int strSize = arraysize(str);

you don't need that function arraysize(). This is where you can use sizeof operator because str is not a pointer.

Edited 4 Years Ago by Ancient Dragon: n/a

I'm not using sizeof() anyway, but i still need to make the declaration of the array to be dynamically in some way, possibly without declaring it as a pointer to a char[].

argh didn't see your reply. So when I'm calling sizeof() in other functions it give me the size of a pointer because the array is passed to the function as a pointer?

If that's true isn't it possible to do this:

void printstr(char str[])
strSize = sizeof(*str);

No, that's not possible either. What is sizeof(*str)? Answer: if str is char* then sizeof(*str) will be 1, which is sizeof(char). There is no way to get the allocated size a pointer. You can call strlen() to get the length of the string, but that may or may not the the same as the allocated size.

yes okay, but str is not a pointer to a char array it's just a normal char array.

in main(), yes and sizeof will work as expected. In the other functions its just a pointer, even when declared as char str[] in the parameter list.

Okay so i can declare the str[] like this:

char str[] = "someword";

Is it okay to not declare the size of the array? Or do i need to do something like: char str[80].

when you declare char str[] = "Hello"; the compiler allocates the exact amount of memory needed for the string. You can, if you want to, declare it like this: char str[80] = "Hello"; In that case the compiler will allocate 80 bytes of memory, initializing the first few bytes with the string. The second method is best if you know that str may eventually contain more characters, such as if you want to call strcat() to add additional characters or strcpy() to completely overwrite the existing string with something else.

Edited 4 Years Ago by Ancient Dragon: n/a

If i make a strcpy() wouldn't it just allocate the needed memory?

exsample:

main() {
  char str[] = "hello";
  strcpy(str);
}
void strcopy(char str[]) {
  str[] == "helloworld";
}

strcpy() does not allocate any memory, it assumes you know what you are doing and have already allocated the required amount of memory. It also has two arguments, not just one as you posted.

Line 6 of the code you posted is also write. If you want to find out whether str points to "helloworld" then you have to call strcmp(str,"helloworld"); which will return 0 if the two strings are identical.

arh okay i misunderstood, that strcpy() was an existing function. (sorry i just started C, I'm use to string being an actual type.)

But i don't know if you have been running the code, but it is not working as expected, when (str[number]) number is lower than 12, it's not "crypting" the characters. If you know what I'm trying to say? (You will see the problem when running the code) I really can't get to the problem.

But it seems to look like the Decrypt() is working, witch is weird, since they are almost identical.

The only problem is that you have to put a break statement when the letter is found

void crypt(char str[], int strSize) {
	int i; int k;
	
	for (i = 0; i < strSize; i++) {
		for (k = 0; k < ALPHA_SIZE; k++) {
		
			if (str[i] == ALPHA[k]) {
				str[i] = CRYPT_ALPHA[k];
                break;
            }
		}
	}
}

void decrypt(char str[], int strSize) {
	int i; int k;
     
     for (i = 0; i < strSize; i++) {
         for (k = 0; k < CRYPT_ALPHA_SIZE; k++) {
             
             if (str[i] == CRYPT_ALPHA[k]) {
                 str[i] = ALPHA[k];   
                 break;

             }      
             
             
         }    
     }
     
     
     
     
}

Totally makes sense and works 100% but, i don't get why i need it :s Let's say:

str[0] = 'e';

When it goes through the second loop, after str[0] is set to 'e' and it continues the loop (if the break statement wasent there) it shouldn't change str[0] to anything because the 'if' statement inside the second loop shouldn't be true?

Lets say you want to decrypt and the original value of str[0] == 'v'. When the loop gets to 5 it will change str[0] to 'e' and continue the loop. Now its going to look for 'e' instead of 'v', and will find it when the loop gets to 21 and change it back to a 'v'. something similar may or may not happen with all the other letters. The break statement will stop the loop when the letter is first found.

aaaargh of cause! :D Thanks a lot for the help, really great full :)

This question has already been answered. Start a new discussion instead.