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 7 years ago.
Improve this question
code here:
#include <stdio.h>
#define ROWS 6
#define CHARS 10
int main(void)
{
int row;
char ch;
for(row = 0; row < ROWS; row++)
{
printf("%d\n", row);
for(ch = 'A'; ch < ('A' + CHARS); ch++)
printf("%c", ch);
printf('\n');
}
getchar();
return 0;
}
output here:
0
ABCDEF
i think the output similar like this:
0
ABCDEF
1
ABCDEF
2
ABCDEF
3
ABCDEF
4
ABCDEF
5
ABCDEF
question is why loop just one time.
"ABCDEF" contains 6 chars, so you need to change
#define CHARS 10
to
#define CHARS 6
Also, the printf takes a string, so you should use "\n" instead of '\n'.
#include <stdio.h>
#define ROWS 6
#define CHARS 6
int main(void)
{
int row;
char ch;
for(row = 0; row < ROWS; row++)
{
printf("%d\n", row);
for(ch = 'A'; ch < ('A' + CHARS); ch++)
printf("%c", ch);
printf("\n"); // Should use double quotes here
}
getchar();
return 0;
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I don't know how to explain my need using only words, so let's see an example and check for details.
I want to generate passwords with length of 5 Letters only Upper/Lower case inside my program like this:
aaaaa
aaaab
aaaac
.....
AAAAA
AAAAB
.....
bbbba
bbbbb
.....
Try all combination begining from 'aaaa' until 'ZZZZZ'.
Mixed upper/lower case is allowed and required for my use.
I got this code, but it don't supporte Upper case (I want mixed Lower and Upper case).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isFinish(char *str){
return '\0'== str[strspn(str, "z")];
}
void inc_str(char *str){
int index, carry;
for(index = strlen(str)-1;index>=0;--index){
if(str[index] == 'z'){
carry = 1;
str[index] = 'a';
} else {
carry = 0;
str[index] += 1;
}
if(carry == 0)break;
}
}
int main(){
int n;
char *str;
n=7;//length
str=(char*)malloc(sizeof(char)*(n+1));
//initialize
memset(str, 'a', n);//"aa..aa"
str[n]='\0';
while(1){
printf("%s\n", str);
if(isFinish(str))
break;
inc_str(str);
}
free(str);
return 0;
}
source: https://stackoverflow.com/a/10161603/11622790
I modified this code with the help of #L. Scott Johnson to supprt UPPER/LOWER case.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int isFinish(char *str){
return '\0'== str[strspn(str, "Z")];
}
void inc_str(char *str){
int index, carry;
for(index = strlen(str)-1;index>=0;--index){
if(str[index] == 'Z'){
carry = 1;
str[index] = 'a';
} else if(str[index] == 'z'){
carry = 0;
str[index] = 'A';
} else {
carry = 0;
str[index] += 1;
}
if(carry == 0)break;
}
}
int main(){
int n;
char *str;
n=7;//length
str=(char*)malloc(sizeof(char)*(n+1));
//initialize
memset(str, 'a', n);//"aa..aa"
str[n]='\0';
while(1){
printf("%s\n", str);
if(isFinish(str))
break;
inc_str(str);
}
free(str);
return 0;
}
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 6 years ago.
Improve this question
How do you split a string:
char *mystring = "12345"
into an integer array which looks like this:
[1, 2, 3, 4, 5]
I have tried something like the code below, but I'm not entirely sure if it's reliable, and I think it will be easy to break. This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void) {
char *mystring = "12345";
int string_size, i, length;
string_size = strlen(mystring);
int values[string_size];
for (i = 0; mystring[i] != '\0'; i++) {
values[i] = mystring[i] - 48;
}
length = sizeof(values)/sizeof(*values);
for (i = 0; i < length; i++) {
printf("%d ", values[i]);
}
return 0;
}
Which outputs:
1 2 3 4 5
Is there a more C like way I can do this?
The odd thing I see, which isn't itself a problem, is that you calculate the length of the string/array three different ways:
string_size = strlen(mystring);
for (i = 0; mystring[i] != '\0'; i++) {
length = sizeof(values)/sizeof(*values);
where just one method is sufficient:
#include <stdio.h>
#include <string.h>
int main(void) {
char *mystring = "12345";
size_t length = strlen(mystring);
int values[length];
for (int i = 0; i < length; i++) {
values[i] = mystring[i] - '0';
}
for (int i = 0; i < length; i++) {
printf("%d ", values[i]);
}
printf("\n");
return 0;
}
You can replace 48 with '0' for readability.
You can change all loops to loop until string_size like the first one, no need to change the method for each loop.
And finally if you're going to return that array anywhere outside of local function, you should probably malloc() it rather than use a local/stack variable.
But otherwise, it's pretty simple and it works.
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 6 years ago.
Improve this question
Shouldn't st be a array of pointers to char rather than a pointer to char? I do not understand how the latter for loop prints the value?
int main(void)
{
char temp[256];
char *st;
for (int i = 0; i < 3; i++)
{
scanf("%s", temp);
st= strdup(temp);
}
for(int i=0;i<3;i++)
{
printf("%s",st);
}
}
You probably want this:
#include <stdio.h>
#include <string.h>
int main(void)
{
char temp[256];
char *st[3]; // array of three pointers to char
for (int i = 0; i < 3; i++)
{
scanf("%255s", temp); // prevents potential buffer overflow
st[i] = strdup(temp);
}
for(int i = 0; i < 3; i++)
{
printf("%s\n", st[i]);
free(st[i]); // free strduped memory
}
}
This program displays:
./a.out
11
22
33
11
22
33
Whereas your program displays
./a.out
11
22
33
33
33
33
this is because:
char *st; // in your prog. you only declare one pointer
for (int i = 0; i < 3; i++)
{
scanf("%s", temp);
st= strdup(temp); // here you overwrite the st pointer loosing
// the string strduped in the previous run of the loop
}
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 6 years ago.
Improve this question
How can i store digits and spaces in an array? I am using a char array. Here is my code:
char m[100];
int i;
for(i = 0; i < 5; i++)
if(i == 2)
m[i] = ' ';
else
m[i] = i;
How can i print the content of m? (01 34)
Here this should work for you
#include <stdio.h>
int main(void)
{
char m[100];
int i;
for(i = 0; i < 5; i++)
{
if(i == 2)
m[i] = ' ';
else
m[i] = '0' + i; //<< Note ascii of 0 is 48
}
m[i]= '\0';
printf("%s",m);
return 0;
}
Make sure that you set m[i] = '\0' for one-past the element you want to print (this is the null-terminator), then use a printf like function, with %s as the formatter.
For the digits, you need to use m[i] = '0' + i, else you'll be attempting to print control characters rather than the digits themselves.
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
Can anyone give me a complete example of how to generate an Alphanumeric String randomly
like (ARG534UJ6) using C ? I'm completely new to C.
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
This is not working.
As pointed out by #IngoLeonhardt, use % (sizeof(alphanum) - 1) instead of % sizeof(alphanum)
My guess is that you don't have room for your string, try:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void gen_random(char *s, const int len) {
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i) {
s[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
s[len] = 0;
}
int main(void)
{
char *str = malloc(8 + 1);
/* initialize random seed: */
srand(time(NULL));
gen_random(str, 8);
printf("%s\n", str);
free(str);
return 0;
}