Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
Well, my question is pretty simple, yet i've been stuck for quite a while. I'd like to write a program that takes two arrays as arguments, and then write the letters in it without duplicates, in order of appearance.
example :
$ ./a.out bulwark blue
bulwarke
$ ./a.out fresh feeling
freshling
$ ./a.out final01 test02
final01tes2
I tried a few ways, but can't figure how to do it without malloc. The tricky thing is, I cannot use malloc, the only authorized function is "write"
P.S : Sorry for my bad English
Use an auxiliary array to indicate which letter has already been used:
void func(char arr1[],char arr2[])
{
int hash[256] = {0};
for (int i=0; arr1[i]!=0; i++)
{
unsigned char letter = (unsigned char)arr1[i];
if (hash[letter] == 0)
{
hash[letter] = 1;
printf("%c",letter);
}
}
for (int i=0; arr2[i]!=0; i++)
{
unsigned char letter = (unsigned char)arr2[i];
if (hash[letter] == 0)
{
hash[letter] = 1;
printf("%c",letter);
}
}
printf("\n");
}
Note: this code assumes that each of the input strings (arr1 and arr2) is terminated with a 0 character.
Just keep track what you have printed. The follow will work just fine for char, because there are so few of them. Keeping track of unicode chars with an array that is long enough for all possible values would be mad. In such case inserting identifiers to a hash would probably be better option, but since unicode was not part of the consideration the following should be good enough.
#include <stdio.h>
int main(int argc, char **argv)
{
char arr[256] = { 0 };
int i, j;
for (i = 1; i < argc; i++)
for (j = 0; argv[i][j]; j++)
if (!arr[argv[i][j]]) {
arr[argv[i][j]] = 1;
printf("%c", argv[i][j]);
}
putchar('\n');
return 0;
}
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This code is an exercise from a daily programming mailing list. I am trying to print out a list of given words in reverse order. The words are delimited by spaces. When running the code below, it enters into an infinite loop just printing the (new) first word. I have looked through the conditions and everything looks OK to me. I think it may take a fresh set of eyes to point out a simple mistake, but I can't find anything. Thanks to anyone who can lend a hand.
*Note: I am planning on adding back in the spaces to the output after this is figured out. I am aware the output will just be one long string without spaces so far.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char *words;
words = "here world hello spam foo bar baz";
int space_indices[10];
int counter = 0;
// Find the spaces and keep track of the index numbers in the space_indices array
for (int i = 0; i < strlen(words); i++) {
if (words[i] == ' ') {
space_indices[counter] = i;
counter++;
}
}
for (int i = counter - 1; i >= 0; i--) {
if ( i = counter - 1) {
// Print the first word
for (int j = space_indices[i] + 1; j < strlen(words); j++) {
printf("%c", words[j]);
}
} else if (i >= 0) {
// Print the other words except for the last
for (int j = space_indices[i] + 1; j < space_indices[i + 1]; j++) {
printf("%c", words[j]);
}
} else {
// Print the last word
for (int j = 0; j < space_indices[0]; j++) {
printf("%c", words[j]);
}
}
}
}
As Havenard explained, the problem was that I was not using a comparison operation, I was using an assignment operator.
This:
if ( i = counter - 1)
should be:
if ( i == counter - 1)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have tried to write a sorting function sort(int *buffer, int array[], int size) which works in a way similar to the insertion sort - it takes the first element from the array, sets it as the first element of the buffer and then checks whether or not the next value showing up in the array is greater than the last value stored in the buffer. If yes, it keeps swapping the two elements until everything is in its place. This is my minimal working example:
#include <stdio.h>
void sort(int *buffer, int array[], int size) {
for(int i = 0; i < size; i++) {
buffer[i] = array[i];
while(i >= 1 && buffer[i] < buffer[i-1]) {
int tmp = buffer[i-1];
buffer[i-1] = buffer[i];
buffer[i] = tmp;
printf("i = %d i: %d, i -1 : %d \n",i, buffer[i], buffer[i-1]);
i--;
}
}
}
int main(void) {
int array[3] = {4,3,2};
int buffer[3];
sort(buffer, array, 3);
for(int i = 0; i < 3; i++) {
printf("%d", buffer[i]);
}
}
However, the output of this program is 222
To be honest, I don't see how it's even possible that three identical elements got placed in the buffer.
What can have gone wrong?
You are using the same variable for the inner while cycle and for the outer for loop. Use a different variable and copy the value of i to it in each iteration of the for.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This code need to save friends in some array of array(pointer to pointer) and by the length of the names do realloc (build exactly dynamic place for the strings of each of them) and than prints the string and the length and free everything.
So the code work when i debugging but when I running it with CTR+f5 it's crashed after the fgets of the first string. also all the free loops and the free function doesn't work me here, but if remove it the debugging still work and the CTR+f5 still don't work. help someone?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 20
int main(void)
{
int i = 0, j = 0,friends=0;
char str[LENGTH];
printf("Hello bro,how U doin'?\nTell me how many friends do you have?\n");
scanf("%d",&friends);
char** friendBook = (char**)malloc(sizeof(char)*friends);
if (friendBook)
{
getchar();
for (i = 0; i < friends; i++)
{
*(friendBook+ i) = malloc(LENGTH*sizeof(char));
}
for (i = 0; i < friends; i++)
{
printf("Enter friend number: %d\n", i + 1);
fgets(str, LENGTH, stdin);
str[strcspn(str, "\n")] = 0;
*(friendBook + i) = (char*)realloc(*(friendBook+i),(sizeof(char)*strlen(str))); // dynamic memory for every string(name)
if (*(friendBook + i))
{
strcpy(*(friendBook+i),str);
}
}
for (i = 0; i < friends; i++)
{
printf("Friend: %s\tLength of friend name %d\n", *(friendBook + i), strlen(*(friendBook + i)));
}
}
for (i = 0; i <friends; i++)
{
free(*(friendBook+i));
}
free(friendBook);
system("PAUSE");
return 0;
}
Take the string "Hello". strlen ("Hello") = 5. But to store it, you need SIX bytes, not five, because there is a zero byte at the end that doesn't get counted by strlen.
PS. Undefined behaviour when you try to print strlen with %d format. Can crash, print nonsense, or worse. Use %zd. Turn all warnings on in your compiler and fix them.
PS. BLUEPIXY spotted a much worse problem...
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have here some sample code that has what I think is needed to solve my problem.
Basicaly I need to store inside an array that has a variable amount of entries a variable amount of reasons for it. Is this array[5].b[3] not allowed? Is there any alternative for it?
Thanks in advance
typedef struct {
int a;
char *reasons;
}t_a;
f() {
int space=10,spaceReasons=5;
t_a *array;
array=NULL;
array=realloc(array,sizeof(t_a)*space);
array[5].reasons=realloc(array[5].reasons,sizeof(char)*spaceReasons);
fgets (array[5].reasons[3]),300, stdin);
free(array);
}
Here's a working example:
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int a;
char* reasons;
} t_a;
int main(void) {
int space = 10, spaceReasons = 5;
t_a* array = NULL;
array = realloc(array, sizeof(t_a) * space);
for (int i = 0; i < space; i++) {
array[i].reasons = NULL;
array[i].reasons = realloc(array[i].reasons, sizeof(char) * spaceReasons);
// fill with fgets
fgets (array[i].reasons, spaceReasons, stdin);
}
// print results
for (int i = 0; i < space; i++) {
printf("%s\n", array[i].reasons);
}
// first free all reasons
for (int i = 0; i < space; i++) {
free(array[i].reasons);
}
// then free array
free(array);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to make some batleships, but I don't know if it is posible to display a 2D array for the playing ground?
char arr[SIZE][SIZE];
int i,j;
for(i = 0; i < SIZE; i++){
for(j = 0; j < SIZE; j++){
arr[i][j] = 'O'; //initalizes
printf(" %c ",arr[i][j]); //prints
}
purchar('\n'); //to break every row
}
update spot to X when it has been hit, and print again w/o the initializer line
sorry the code came out weird, but it's basically a nested for loop, each counting to predefined size of game board, can be indexed from 0,SIZE-1
maybe make it a char array and use O and X and then other characters to draw the ships put out, honestly i'd make the ships eight, equals equals and a capital D, but the choice is yours
You're probably looking for a for loop. They usually took something like this:
size_t i; // size_t is an unsigned int large enough to hold a string's length
for(i = 0; i < size_of_my_array; ++i) {
// do stuff with my_array[i]
}
For example, to print the characters in a string individually (not necessarily the most efficient way):
char* name = "Brendan";
size_t i; // size_t is an unsigned int large enough to hold a string's length
for(i = 0; i < strlen(name); ++i) {
printf("%c", name[i]);
}
Looping over other kinds of arrays is similar.