I have written a program which prints all the permutations of a given string. But it was printing some weird things. The code goes as follows:
#include <stdio.h>
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf("%d\n", a);
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j));
}
}
}
int main(void)
{
char a[100];
gets(a);
int k;
k=strlen(a);
permute(a, 0, k-1);
system("pause");
}
It was printing some numbers instead of given string.. plz help
There is your problem:
printf("%d\n", a);
should be
printf("%s\n", a);
Related
I'm new to programming, and i'm trying to complement this code in C to permute strings, currently it shows all the words exchanged and counts how many characters the word has.
But I would like it to count the number of lines generated by the permutation too, and in this part, the code is not working. I do not know what else to do!
Example: The word "hi", generates two lines: hi, and ih. (In this case, i want the program write "generated words: 2")
the code:
#include <string.h>
#include <stdio.h>
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf("%s\n", a);
else
{
for (j = i; j <= n; j++)
{
swap((a + i), (a + j));
permute(a, i + 1, n);
swap((a + i), (a + j)); //backtrack
}
}
}
int main()
{
char str[21];
int len;
int cont = 1;
int fatorial = 1;
printf("\nType a word: ");
scanf("%s", str);
len = strlen(str);
permute(str, 0, len - 1);
printf("\nNumber of letters: %d\n", len);
while (cont < len)
{
fatorial = fatorial * cont;
cont++;
}
printf("\nPossibilities:%d", fatorial);
return 0;
}
You could increment a counter in permute. Something like:
#include <string.h>
#include <stdio.h>
void
swap(char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void
permute(char *a, int i, int n, int *count)
{
int j;
if( i == n ){
printf("%s\n", a);
*count += 1;
} else {
for( j = i; j <= n; j += 1 ){
swap(a + i, a + j);
permute(a, i + 1, n, count);
swap((a + i), (a + j));
}
}
}
int
main(int argc, char **argv)
{
char buf[1024];
char *str = argc > 1 ? argv[1] : buf;
int len;
int contador = 0;
if( argc < 2 ){
printf("\nType a word: ");
scanf("%1023s", buf);
}
len = strlen(str);
permute(str, 0, len - 1, &contador);
printf("\nNumber of words: %d\n", contador);
return 0;
}
i successfully run it on codeblocks, but my clion doesnt display anything when i run it.
i usually use clion, so im very furious about this. (dont want to be obligated to use codeblocks)
please help a poor lost soul.
this is the code.
dont have a lot more to say, but still stackoverflow wants me to write more, so here i am.
#include <stdio.h>
#include <stdlib.h>
int **malloc2dR(int r,int c);
int **MATinit(int r, int c);
void MATstampa(int **m, int r, int c);
void change(int **M, int r, int c);
int main() {
int r=3,c=4;
int **M=MATinit(r,c);
MATstampa(M,r,c);
change(M,r,c);
MATstampa(M,r,c);
return 0;
}
int **malloc2dR(int r, int c){
int **m;
int i;
m=malloc(r*sizeof (int *));
for(i=0;i<r;i++)
m[i]=malloc(c*sizeof (int));
return m;
}
int **MATinit(int r, int c){
int **M=malloc2dR(r,c);
int i,j;
printf("scrivere in input i valori della matrice %dx%d\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&M[i][j]);
return M;
}
void MATstampa(int **m, int r, int c){
int i,j;
for(i=0;i<r;i++) {
for (j = 0; j < c; j++)
printf("%d ", m[i][j]);
printf("\n");
}
printf("\n");
}
void change(int **M, int r, int c) {
int i, j;
int ii, jj;
int **Mfake=malloc2dR(r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
Mfake[i][j]=M[i][j];
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
if (M[i][j] % 2 == 1) {
for (ii = 0; ii < r; ii++)
Mfake[ii][j] = 1;
for (jj = 0; jj < c; jj++)
Mfake[i][jj] = 1;
}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
M[i][j]=Mfake[i][j];
}
This program is supposed to take an array, and sort it from lowest to highest value. My program won't sort any values though. I believe the error is in the selectionSort. The values i and j are present in the function, I printed them out inside the function but they are not passed into the swap function. I tried making i and j pointers but it didn't work. I just have no clue on what to do next. Any help would be appreciated.
#include <stdio.h>
#define N 5
void selectionSort(int *a, int n);
int *findLargest(int *a, int n);
void swap(int *p, int *q);
int main(void)
{
int i;
int a[N];
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
selectionSort(a, N);
printf("In sorted order:");
for (i = 0; i < N; i++) {
printf(" %d", a[i]);
}
printf("\n");
return 0;
}
void selectionSort(int *a, int n)
{
int *p = a;
int i;
int j;
if (n == 1) {
return;
}
i = *(p+n-1);
j = *findLargest(a, n);
swap(&i, &j);
selectionSort(a, n - 1);
}
int *findLargest(int *a, int n)
{
int *p;
int *p_max = a;
for(p = a + 1; p < a + n - 1; p++) {
if ( *p > *p_max)
p_max = p;
}
return p_max;
}
void swap(int *p, int *q)
{
int temp = *(p-1);
*(p-1) = *q;
*q = temp;
}
The problem is in your call of swap: you swap the content of two local variables
int i;
int j;
... // Some other code, then
swap(&i, &j);
This has no effect on the original array. You should be passing p+n-1 and findLargest(a, n) directly, or store their results in pointers, not in ints:
swap(p+n-1, findLargest(a, n));
In addition, your swap is broken: rather than swapping the content of two pointers, it assumes that p points one element past the target location. This is a bad assumption to make in a general-purpose function, such as swap, and it also leads to undefined behavior in your program.
void swap(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}
I'm trying to generate permutations of a string and store them in a pointer array
#include<stdio.h>
#include<string.h>
int count;
char *str[100];
/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
/* Function to print permutations of string
This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
void permute(char *a, int i, int n)
{
int j;
if (i == n)
{
str[count++]=a;
printf("%s\n", str[count-1]);
}
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j)); //backtrack
}
}
}
/* Driver program to test above functions */
int main()
{
char a[] = "ABC";
int i;
count=0;
permute(a, 0, 2);
for(i=0;i<count;i++)
printf("%s\n", str[i]);
return 0;
}
When I execute this, I get output as:
ABC ACB BAC BCA CBA CAB
ABC ABC ABC ABC ABC ABC
Why am i getting these different values for str in different functions?
Solved:
str[count++]=strdup(a);
or
converted *str[] to str[][] and then did strcpy(str[],a)
You are editing 'a' in place. You should make a copy of each resulting permutation.
str[count++]=strdup(a);
The code given below prints all the possible combination of a given string. It produces strings recursively . Now i want to store each combination in an array using an array of pointer to each string . How do i initialize the pointer so that it points to string.
The code is :-
Input ABC
Output
ABC in b[0]
ACB in b[1]
BAC
BCA
CAB
CBA
and so on .
thanks :)
void permute(char *a, int i, int n)
{
int k=0;
char *b[100];
int j;
if (i == n)
{
// *b[k]=a;
printf("%s\n", a);
i++;
}
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j)); //backtrack
}
}
}
There is no point in pointing elements of b to a, since is a variable string (that is, it keeps changing). Possible output of such code would be that all elements of b would be last permutation of the string a.
You need dynamic allocation of string each time you find a new permutation.
That you can do manually using malloc(). Or can use strdup() to create duplicate strings for you (Remember to free() them at the end ofcourse).
/*Sample Code*/
if (i == n)
{
b[k] = strdup(a);
...
}
Remember, that you also need to pass k as the argument to the function permute(), since k is an automatic variable, newly created with value = 0, each time function permute() is called. There are other possibilities, make k global or static variable.
You could dynamically allocate an array that would hold individual char arrays (or C strings) representing each permutation. One of the things that would make this code generic is to find the value of total_permutations in the main() for a given string with strlen N, which would actually be factorial(N). Here:
void swap(char* A, char* B) {
char t;
t = *A;
*A = *B;
*B = t;
}
int permute(char **arr_of_chars, int count, char *a, int i, int n)
{
int k=0;
char *b[100];
int j;
if (i == n) {
// *b[k]=a;
printf("%s\n", a);
memcpy(arr_of_chars[count], a, strlen(a));
count++;
i++;
} else {
for (j = i; j <= n; j++) {
swap((a+i), (a+j));
count = permute(arr_of_chars, count, a, i+1, n);
swap((a+i), (a+j)); //backtrack
}
}
return count;
}
int main() {
char str[] = "";
char **arr_of_str = NULL;
int len_str = strlen(str);
int i = len_str;
int total_permutations = 1;
while (i > 0) { /* Get all the combinations */
total_permutations *= i;
i--;
}
arr_of_str = (char **) malloc(total_permutations * sizeof(char*));
for (i=0; i <total_permutations; i++) {
arr_of_str[i] = (char *) malloc(sizeof(char) * len_str);
}
permute(arr_of_str, 0, str, 0, (len_str-1));
for (i=0; i <total_permutations; i++) {
printf("%s \n", arr_of_str[i]);
free(arr_of_str[i]);
}
free(arr_of_str);
}