i have created ac program to print all possible strings of 8 characters. But the printf() function inside the program does not work at all. Here's the program
#include <stdio.h>
#include <string.h>
#include <conio.h>
void ini(void);
void check(void);
void add(void);
static char str[9]; //permuting string
static char test[9]; //test string
static short int c = 1;
void print(char*);
void print(char a[]) {
short int i,ln = (int)(strlen(str)-1);
for(i = 0; i <= ln; i++)
printf("%c",a[i]);
printf("n");
}
void ini() {
//initialzing the strings.
short int i = 0;
for(i = 0; i < 9; i++){
str[i] = 'A';
test[i]= 'z';
}
puts("ini.....");
//initializing done
}
void check() {
//check if the strings 'str' and 'test' are equal
c = strcmp(str, test);
puts("checking..........");
}
void add() {
//this is the heart of the program
short int i = 0;
for( i = 7; i > 0; i--)
{
if((int)str[i] >= (int)'z') {
str[i] = 'A';
str[i-1]++;
}
else if(str[i] < 'z'){
str[i]++;
break;
}
}
puts("adding.......");
}
int main() {
//now we execute the functions above
puts("in main.....");
int i = 0;
while( c != 0 ) //execute the loop while the permuting string 'str' is not equal to the final string 'test'
{
puts("inside while.......");
for(i = 65; i <= 122; i++) { //loop to make the last character of 'str' permute from 'a' to'Z'
str[7] = (char)i;
puts("inside for");
print(str); //print the whole string to the screen
}
add(); //change the next char
check(); //check to see if 'str' has reached it's final point.
}
return 1;
getch();
}
and here's the result.............
The program enters the for loop in main() but it does not execute the print() function. I have tried printf() but that shows the same result. What am I doing wrong?
You decleared,
static char str[9];
So, all elements in str
is NULL
( \\0
) now. Then,
ln = (int)(strlen(str)-1);
Here, strlen
counts characters until a \\0
found. So, as the first character in str
is \\0
, ln
will be -1
. In the for loop
,
for(i = 0; i <= ln; i++)
i <= ln
condition will fail.
You may try, in the main
function,
str[0] = (char) i;
or, call ini()
before calling print(str);
But careful, your while loop
will never break! As c
's never gonna be 0
.
Because your code does not changing the 9th character from A
to z
so strcmp(str, test) is not going to return 0
.
In your case, str
is a global static array, all elements are initialized to 0.
Next, in your code, you're only setting the index 7 and trying to pass the array to print()
function. But the fact is, the very first element is a null
(NOte: not a NULL
, it's null
which is 0
or '\\0'
). So, the call to strlen()
inside print()
will return a 0
. So, the immediate printf()
will print nothing.
That said, I think, the next printf()
you meant to write as printf("\\n");
TL;DR Solution: You're missing a call to ini()
in your main()
at the start, please add that.
It looks like I did not call the ini() function from the main(). Calling it will initialize the strings so that the null byte is moved to the end. After that the program works just fine. So question closed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.