I'm trying to fill a string with characters from the string 'reset\0' but randomized. For some reason it only gives me one character bacK:
#define STR_LEN 6
char *inputString()
{
char *string[STR_LEN + 1] = {0};
const char *digits = "reset\0";
int i;
for (i = 0; i < STR_LEN; i++)
{
string[i] = digits[ rand() % 5 + 0 ];
}
printf("STRING: %s\n", string);
}
prints only one character like 't' or 'e' to the console. What am I doing wrong?
If you mean making a random permutation of characters, not just using the source string as an alphabet, consider Fisher–Yates shuffle. An implementation could be like this:
char s[] = "reset";
for (size_t i = strlen(s) - 1; i > 0; i--)
{
size_t j = rand() % (i + 1);
char t = s[i];
s[i] = s[j];
s[j] = t;
}
printf("%s\n", s);
The idea is to go from right to left, and on every step to swap the element at index i with an element at random index j between 0 and i, inclusive.
Corrected code
#define STR_LEN 5
char *inputString()
{
char string[STR_LEN + 1] = {0};
const char *digits = "reset";
int i;
for (i = 0; i < STR_LEN; i++)
{
string[i] = digits[ rand() % 5 + 0 ];
}
printf("STRING: %s\n", string);
}
But note that this doesnt do what you want, it print 5 random chars from 'reset' so 'ssere' can be output
Related
I want to repeat a string - for example hello - for a specific number of imes - for example 3 times -, but it doesnt work :) The example should look like this: hellohellohello, but I get no output or i get HHHHHHHHHHH...
here is my code:
char *repeat_str(size_t count, char *src) {
int length = strlen(src);
int z = length;
char *ausgabe = calloc((length*(count+1)), sizeof(char));
for(int i = 0; i<=((int) count);i++){
for(int j =0; j< length; j++){
ausgabe[i+j+z] = src[j];
}
z=z*2;
}
//printf("%s\n", ausgabe);
return(ausgabe);
}
If i remove the 'z' in the brackets of 'ausgabe', i get the output HHHHHHHH%, with the z I just get no output. Could bdy pls help me change this behavoiur - and more important, understant why it does that?
As you are always referring *src, which is fixed to the first letter of src,
the result looks like repeating it. Would you please try instead:
char *repeat_str(size_t count, char *src) {
int length = strlen(src);
char *ausgabe = calloc(length * count + 1, sizeof(char));
for (int i = 0; i < count; i++) {
for (int j = 0; j < length; j++) {
ausgabe[i * length + j] = src[j];
}
}
//printf("%s\n", ausgabe);
return ausgabe;
}
The strcat function is your friend. We can calloc a buffer long enough for n source strings, plus one for the null terminator, and then just concatenate the source string onto that buffer n times.
char *repeat_string(int n, const char *s) {
int len = strlen(s) * n + 1;
char *result = calloc(len, 1);
if (!result) return NULL;
for (int i = 0; i < n; i++) {
strcat(result, s);
}
return result;
}
I have an array of 64 characters, which I need to divide into two parts, the left part of 32 characters and the right part, also 32 characters.
char *IP_M; // 64 characters array
char L[32]; // left part
char R[32]; // right part
The IP_M array is filled in as follow:
char *start_inital_permutation(const char *input) {
char *output = malloc(64 * sizeof(char));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
output[i * 8 + j] = input[IP[i][j] - 1];
}
}
return output;
}
...
IP_M = start_inital_permutation(M);
where M is also a 64 characters string. With the following method I tried to fill the other two array (L, R) by spliting the IP_M.
void fill_LR() {
for (int i = 0; i < 32; i++) {
L[i] = IP_M[i];
R[i] = IP_M[i + 32];
}
}
but when I run the following instructions:
printf("IP_M: %s\n", IP_M);
printf("L: %s\n", L);
printf("R: %s\n", R);
the output is:
IP_M: 1100110000000000110011001111111111110000101010101111000010101010
L: 1100110000000000110011001111111111110000101010101111000010101010
R: 11110000101010101111000010101010
I can't get out of this situation, can someone help me please?
*EDIT: also tried the memcpy() method but it still not work!
Here is the Project if someone want to see it:
https://github.com/ionutbogdandonici/DES_C.git
Strings in C are \0 terminated. So the print function will print the string until it reaches the \0 character.
Assign space for null:
char L[33]; // left part
char R[33]; // right part
Add null terminator:
void fill_LR() {
for (int i = 0; i < 32; i++) {
L[i] = IP_M[i];
R[i] = IP_M[i + 32];
}
L[32] = 0;
R[32] = 0;
}
output[i * 8 + j] = input[IP[i][j] - 1]; is gibberish.
Strings in C are null terminated but you never allocate space for a null terminator anywhere, nor do you null terminate your strings.
Don't use global variables.
I was able to salvage your program like this:
#include <stdio.h>
#include <stdlib.h>
char *start_inital_permutation(const char *input) {
size_t count=0;
char *output = malloc(64 * sizeof(char) + 1);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
output[i * 8 + j] = input[count++];
}
}
output[64] = '\0';
return output;
}
int main()
{
const char input[] = "1100110000000000110011001111111111110000101010101111000010101010";
char *IP_M = start_inital_permutation(input);
char L[32+1]; // left part
char R[32+1]; // right part
for (int i = 0; i < 32; i++) {
L[i] = IP_M[i];
R[i] = IP_M[i + 32];
}
L[32] = '\0';
R[32] = '\0';
printf("IP_M: %s\n", IP_M);
printf("L: %s\n", L);
printf("R: %s\n", R);
}
However, there's no apparent reason why you need to do the middle step with the 64 characters array. You could as well put that one in a union and save the copy (although then the individual left/right strings won't be null terminated). Example:
#include <stdio.h>
#include <stdlib.h>
typedef union
{
char data [64+1];
struct
{
char left[32];
char right[32];
char zero;
};
} ip_t;
ip_t *start_inital_permutation(const char *input) {
size_t count=0;
ip_t* obj = malloc(sizeof(ip_t));
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
obj->data[i * 8 + j] = input[count++];
}
}
obj->data[64] = '\0';
return obj;
}
int main()
{
const char input[] = "1100110000000000110011001111111111110000101010101111000010101010";
ip_t *IP_M = start_inital_permutation(input);
printf("IP_M: %s\n", IP_M->data);
printf("L: %.32s\n", IP_M->left);
printf("R: %.32s\n", IP_M->right);
}
Using printf with "%s" assumes the value is a zero terminated string (AKA NULL terminated string).
I.e. a pointer to a sequence of chars, ending with a \0 char.
In your case when printf attempts to print L it prints char, and after the 32 chars that belong to L it continues. It happened to be that R is following L in memory, and so the content of R is also dumped. If the next byte in memory following R was not a 0, you would see even more characters printed. This behavior is dependent on the [possibly atrbitarary] content of your memory.
How to handle the issue (2 ways):
1. You can either increase the size of L and R to 33, and assign the last char to \0:
char L[33]; // left part
char R[33]; // right part
/* ... */
L[32] = '\0';
R[32] = '\0';
2. Or specify to printf the length of the strings (32) like this:
/*----------vvv-------*/
printf("L: %.32s\n", L);
printf("R: %.32s\n", R);
In the later case keep in mind that L and R are not "regular" C strings, which are expected to be zero terminated (at least as far as it concerns common functions like strlen, strcmp etc.).
I'm trying to create a function that converts an integer into a string, basically what have I done is the following functions: when we get the numbers from the conversion they are reversed so I need a reverse function to make them in the right way. The intostring uses (I think? I got it from some website) the ascii table to convert the number into the string desired.
The problem is: when I enter the 2-digit number they are reversed the wrong way (I guess my reverse function doesn't work that well) and after a certain number of digit the conversion it's not any more accurate.
reverse function:
char reverse(char *stringa) {
int len = strlen(stringa) - 1;
for(int i = 0; i < len / 2; i++) {
char tmp = stringa[i];
stringa[i] = stringa[len - i];
stringa[len - i] = tmp;
}
}
intostring function:
void intostring(int num, char *str) {
int i = 0;
while (num != 0) {
int rem = num % 10;
str[i++] = (rem > 9)? (rem-10) + 'a' : rem + '0';
num = num / 10;
}
str[i] = '\0';
reverse(str);
}
The condition i<len/2 in the reverse function is wrong.
For example, if the string is 2-digit long, len will be 1 and len/2 will be 0. Therefore, no swap will occure while the two characters should be swapped.
the condition should be i<=len/2 or i<len-i.
/*
It works clearly . Checked.
*/
void reverse(char source[],char destination[]) {
int x,i;
//start from last char
i = i=(strlen(source)-1
for (x=0;x<strlen(source);x++){
//Insert char at i in source to x in destination
destination[x]=source[i];
destination[x]='\0';
i--;
}
}
There are multiple problems:
the reverse function fails for an empty string. You should not subtract 1 from the length, but adjust the offset inside the loop.
reverse is defined to return a char but does not return anything. Make it return a char * and return stringa.
intostring produces an empty string for num <= 0. You should loop while num > 9 and add the final digit after the loop.
intostring converts the digit into a character for bases up to 36 (assuming ASCII). This is unnecessarily complex since the base is 10. Use a simpler conversion: str[i++] = '0' + rem;
it may be useful for intostring to return a pointer to the destination array.
Here is a modified version:
#include <string.h>
char *reverse(char *str) {
size_t len = strlen(str);
for (size_t i = 0; i < len / 2; i++) {
char tmp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = tmp;
}
return str;
}
char *intostring(int num, char *str) {
int i = 0;
if (num >= 0) {
while (num > 9) {
str[i++] = '0' + num % 10;
num = num / 10;
}
str[i++] = '0' + num;
} else {
while (num < -9) {
str[i++] = '0' - num % 10;
num = num / 10;
}
str[i++] = '0' - num;
str[i++] = '-';
}
str[i] = '\0';
return reverse(str);
}
Here is an alternative approach for the reverse function using 2 index variables:
#include <string.h>
char *reverse(char *str) {
size_t i = 0;
size_t j = strlen(str);
while (j --> i) {
char c = str[j];
str[j] = str[i];
str[i++] = c;
}
return str;
}
The reverse function condition is worng.
If the integer in 32 then the string will be
s[0] = '2', s[1] = '3', s[2] = '\0' before string reversal.
so in reverse function the following swap operation has to be applied as
if number = 32 then len = 2
i = 0 then len - i - 1 = 1
so 0 and 1 will be swaped.
void reverse(char *stringa){
int len = strlen(stringa);
for(int i = 0; i < len / 2; i++){
char tmp = stringa[i];
stringa[i] = stringa[len - i - 1];
stringa[len - i - 1] = tmp;
}
}
void intostring(int num, char *str)
{
int i = 0;
if(num == 0){
str[i++] = '0';
str[i] = '\0';
}
else if(num > 0){
while(num != 0){
int rem = num % 10;
str[i++] = '0' + rem;
num = num/10;
}
str[i] = '\0';
}
else{
while(num != 0){
int rem = num % 10;
/*
(-5/2) => -2
-2 * 2 => -4
so a%b => -1
(5/-2) => -2
-2 * -2 => 4
so a%b => 1
*/
rem = abs(rem); // as the rem value is negative
str[i++] = '0' + rem;
num = num/10;
}
str[i++] = '-';
str[i] = '\0';
}
reverse(str);
}
Your reverse function is wrong. You can make it correct (and readable) like below:
void reverse(char *stringa) {
int i = 0; //Forwarding moving index
int j = strlen(stringa) - 1; // the end index
int tmp; // Edited to get the code compiled
for (; i < j; i++, j--) {
tmp = stringa[i];
stringa[i] = stringa[j];
stringa[j] = tmp;
}
}
/*implementation of strrev i.e. string reverse function*/
#include<stdio.h>
#include<string.h>
/*length of the string i.e. cells in the string*/
static const unsigned int MAX_LENGTH = 100;
//static const int MAX_LENGTH = -100;
/*reverses the string*/
void reverseString(char[]);
/*swaps the elements in the cells of a string*/
void swap(char[], int, int);
/*runs the program*/
int main()
{
char string[MAX_LENGTH];
//char string[0]; //no error!
//char string[-1]; //error!
gets(string);
reverseString(string);
printf("\n%s", string);
return 0;
}
void reverseString(char string[])
{
int i;
for(i = 0; i < (strlen(string) / 2); i++)
//for(i = 0; i <= ((strlen(string) - 1) / 2); i++)
{
swap(string, i, (strlen(string) - 1 - i));
}
}
void swap(char string[], int i, int j)
{
int temp = string[i];
string[i] = string[j];
string[j] = temp;
/*
string[i] = string[i] + string[j]; //i = i + j
string[j] = string[i] - string[j]; //j = i + j - j = i
string[i] = string[i] - string[j]; //i = i + j - i = j
*/
}
Look at the "reverseString" and "swap" functions. The current code works perfectly. If the swap function is rewritten by using no "temp" variable, code still runs smoothly.
However if the "for" line in "reverseString" function is replaced with the code just below it (commented using single-line comment), the code doesn't work for single character strings if modified swap function (without temp) is used, but works if original swap function (with temp) is used.
Why is this behavior seen?
The version of the swap function without a temp depends on the two indexes being different from each other.
Suppose i and j are the same. You then effectively have the following:
string[i] = string[i] + string[i]; // string[i] is now 2 * string[i]
string[i] = string[i] - string[i]; // string[i] is now always 0
string[i] = string[i] - string[i]; // still 0
So swapping an element with itself will zero it out.
Now looking at the loop in reverseString when you use i < (strlen(string) / 2) as your condition. If the length of the string is odd, the loop stops before reaching the middle element, so swapping an element with itself doesn't happen.
But when i <= ((strlen(string) - 1) / 2) is your condition, the loop does operate on the middle element which subsequently gets swapped with itself and gets zeroed out.
I would suggest less strlen calls. Your code it IMO too complicated
char *reverse(char *str)
{
char *saved = str;
size_t len = strlen(str);
char *end = str + len - 1;
for(size_t index = 0; index < len / 2; index++)
{
char tmp = *str;
*str++ = *end;
*end -- = tmp;
}
return saved;
}
or
char *reverse1(char *str)
{
char *saved = str;
size_t len = strlen(str);
char *end = str + len - 1;
while(str < end)
{
char tmp = *str;
*str++ = *end;
*end -- = tmp;
}
return saved;
}
why to return char * instead of void. it allows you yo use the functions directly in another operations. For example:
char str[] = "Hello World";
printf("%s\n", reverse(str));
It does not work because you cannot use this code:
string[i] = string[i] + string[j]; //i = i + j
string[j] = string[i] - string[j]; //j = i + j - j = i
string[i] = string[i] - string[j]; //i = i + j - i = j
to swap the value of the variable with itself, since it's the same variable, not two different variables, and the value gets overwritten.
I would strongly recommend you start using C++, and use std::swap() every time you need something to be swapped =)
I'm trying to writing a function to reverse a string: If the string input is "Hello World", the function should return "dlroW olleH". However, when I run my function the string stays the same:
void reversestring(char* s) {
char tmp; //tmp storing the character for swaping
int length; //the length of the given string
int i; //loop counter
//reverse the string of even length
length = strlen(s);
if (length % 2 == 0) { //if the length of the string is even
for(i = 0; i < (int) (length / 2);i++) {
tmp = s[length - i];
s[length - i] = s[i];
s[i] = tmp;
}
}
//reverse the string of odd length
if (length % 2 == 1) { //if the length of the string is odd
for(i = 0; i < (int) ((length + 1) / 2);i++) {
tmp = s[length + 1];
s[length + 1] = s[i];
s[i] = tmp;
}
}
}
You only need one loop to deal with strings. And the symmetric character of s[i] is s[length-i-1],
void reverse(char* s) {
char tmp; //tmp storing the character for swaping
int length; //the length of the given string
int i; //loop counter
//reverse the string of even length
length = strlen(s);
if (length < 2) return;
for(i = 0; i < (int) (length / 2);i++){
tmp = s[length - i - 1];
s[length - i - 1] = s[i];
s[i] = tmp;
}
}
Exemple:
abcde
01234
length is 5, length / 2 is 2 (integer division). length is odd, but you don't have to move the central character. Characters that need to be swapped
(0,4), (1,3)
Test:
int main () {
char x[] = "Hello World";
reverse(x);
printf("%s \n",x );
return 0;
}
prints
dlroW olleH
You are off by one in indexing. The symmetric of s[0] is not s[length- 0] , but s[length-0-1].
As for the odd case I don't get it what exactly you try to do but it seems you easily go out of bounds on each iteration.