Reputation: 51
A snippet of a piece of my code. My function below accepts text from the user. Reverses the entire input, then attempts to store the ASCII representation of the reversed string in an array called ascii_array
. However, as the code stands it just accepts the first character of the reversed string, increments and overwrites the first position of the ascii_array
. I've tried several ways to increase the pointer to store the characters in ascii_array
but I get an error. The array should contain the ASCII representation of the reversed string in ASCII array.
Example: Input: hello!
Ascii representation of the reversed string: !elloh
Expected Output: 33 111 108 108 101 104
Output I get: 33
#include<string.h>
#include<stdio.h>
void encrypt(char inputText[], int inputLength);
int main(void)
{
char word[512];
int length;
printf("Enter word: ");
fgets(word,512,stdin);
length = strlen(word);
encrypt(word,length);
return 0;
}
void encrypt(char inputText[], int inputLength)
{
printf("%s\n",inputText);
char *begin = inputText;
char *end = inputText + inputLength - 1;
char temp;
while(end > begin)
{
temp = *end;
*end-- = *begin;
*begin++ = temp;
}
char *ascii_pointer;
char temporary;
ascii_pointer = inputText + 1;
temporary = *ascii_pointer;
int *p;
int array[512];
p = array;
while(ascii_pointer < inputText + inputLength)
{
printf("INPUTTEXT: ascii_pointer: %s\n " , ascii_pointer);
temporary = *ascii_pointer++;
printf("temporary: %c\n " , temporary);
*p = temporary++;
// I think my logic is incorrect fron this point.
//*p++ = temporary++;
//*p++;
printf("asci_pointer: %c\n " ,*p);
printf("ascii_array: %d\n ", *array);
printf("\n");
}
printf("\n");
printf("END: %p:%c\n", p, *p);
printf("END--- MAYBE GARBAGE VALUE: place holder: %c\n ",temporary);
printf("END: ascii_array: %d\n ", *array);
return;
}
Upvotes: 0
Views: 110
Reputation: 164809
I want to have an array of integers. Because later on in the code I will be adding values to those integers.
If you want to convert a char *
into an array of integers there isn't much work to be done. A char
is already a 1 byte integer. You can simply leave it as is and do math on it.
For example...
int main(void) {
char string[] = "Hello";
// Iterate through the string using the pointer until we see null.
// It avoids having to use strlen() which scans the whole string.
for( char *tmp = string; tmp[0] != '\0'; tmp++ ) {
// Print the char as an integer.
printf("%d\n", tmp[0]);
}
}
$ ./test
72
101
108
108
111
If you want to manipulate those numbers, go ahead! Here's an example that adds 2 to each character.
int main(void) {
char string[] = "Hello";
// Iterate through the string using the pointer until we see null.
// It avoids having to use strlen() which scans the whole string.
for( char *tmp = string; tmp[0] != '\0'; tmp++ ) {
tmp[0] += 2;
}
puts(string);
}
$ ./test
Jgnnq
Just keep in mind that char
is a single byte and can only store up to 127.
So all your encrypt function needs to do is reverse the string.
void encrypt(char inputText[], int inputLength)
{
printf("%s\n",inputText);
char *begin = inputText;
char *end = inputText + inputLength - 1;
char temp;
while(end > begin)
{
temp = *end;
*end-- = *begin;
*begin++ = temp;
}
}
If you really want to convert a string to an array of integers, a simple loop will do.
int main(void) {
char string[] = "Hello";
// Allocate an integer array to hold each integer in the string.
// This ignores the null byte.
int *copy = malloc( strlen(string) * sizeof(int) );
size_t len = strlen(string);
// Simple iteration to copy the string. A char will always
// be smaller than an int so the assignment just works.
for( size_t i = 0; i < len; i++ ) {
copy[i] = string[i];
}
// Print out each integer in the new array.
for( size_t i = 0; i < len; i++ ) {
printf("%d\n", copy[i]);
}
free(copy);
}
Upvotes: 3
Reputation: 1175
You should try this code and understand where you did some errors (see comments):
void encrypt(char inputText[], int inputLength)
{
printf("%s\n",inputText);
char *begin = inputText;
char *end = inputText + inputLength - 1;
char temp;
while(end > begin)
{
temp = *end;
*end-- = *begin;
*begin++ = temp;
}
char *ascii_pointer;
char temporary;
// ascii_pointer = inputText + 1; => you miss the first character
ascii_pointer = inputText;
temporary = *ascii_pointer;
int *p;
int array[512];
p = array;
while(ascii_pointer < inputText + inputLength)
{
printf("INPUTTEXT: ascii_pointer: %s\n " , ascii_pointer);
temporary = *ascii_pointer++;
printf("temporary: %c\n " , temporary);
// *p = temporary++; => you always write at the first array position
*p++ = temporary++;
printf("ascii_array: %d\n ", *array);
printf("\n");
}
printf("\n");
printf("END: %p:%c\n", p, *p);
printf("END--- MAYBE GARBAGE VALUE: place holder: %c\n ",temporary);
// printf("END: ascii_array: %d\n ", *array); => you print only the first array element
printf("END: ascii_array: ");
p = array;
while (p < array + inputLength)
printf("%d ", *p++);
printf("\n");
return;
}
Which will give you for "hello!":
END: ascii_array: 33 111 108 108 101 104
In the future, you should try to do this with a lot less lines :)
Upvotes: 1