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.
Related
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 3 years ago.
Improve this question
I try to print out the atoi value from the function that is shown in The C Programming Language but I found out that the previous format for main cant print out the atoi value.
Previously, I have write htoi in C with this format but this time it don't work. The code is shown below:
#include <stdio.h>
#include <ctype.h>
#define MAXLINE 1000
int get_line(char line[], int maxline);
int atoi(char s[]);
int main(void)
{
int len;
char line[MAXLINE];
while ((len = get_line(line, MAXLINE)) > 0) {
atoi(line);
printf("%s", line);
}
return 0;
}
int get_line(char s[], int lim)
{
int c, i;
for (i = 0; i < lim-1 && (c=getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int atoi(char s[])
{
int i, n, sign;
for (i = 0; isspace(s[i]); i++)
;
sign = (s[i] == '-') ? -1 : 1;
if (s[i] == '+' || s[i] == '-')
i++;
for (n = 0; isdigit(s[i]); i++)
n = 10 * n + (s[i] - '0');
return sign * n;
}
It copy the user's input instead of print out the int value converted by atoi which is not my desired result.
You need to assign the result of atoi() to a variable and print that.
while ((len = get_line(line, MAXLINE)) > 0) {
int num = atoi(line);
printf("%d\n", num);
}
As you wrote in your code, atoi returns an int. At that point you have two solutions :
A . You store the returned value in a variable then you print it.
B . You print it without storing it using :
printf("%d",atoi(string));
Note : if you weren't aware of its existence, there is a already a function atoi() in stdlib.h.
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 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;
}
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
Hi,
I am trying to read a string into my code via the args[]-parameter, like I would do in Java.
So basically, this is what I want to do:
- read the String "machine" over launch-parameter
- go through every letter of that string in a loop
- while in the loop, check is current letter equals "e"
- if letter equals "e", replace it with "a"
- return edited string
This is the best way to phrase my elemental questions to C. So I'd be happy if you won't take this post offensive.
How could I implement that code?
Here's a solution that (almost) doesn't involve pointers, though you should really learn about pointers if you're going to do even moderately advanced C programming.
void replace_e_with_a(char str[])
{
int i, len = strlen(str);
for (i=0; i<len; i++) {
if (str[i] == e) str[i] = a;
}
}
int main(int argc, char *argv[]){
int i;
for (i = 1; i < argc; i++) {
replace_e_with_a(argv[i]);
puts(argv[i]);
}
}
Here's something that should work.
#include <stdio.h>
void replace_e_with_a(char * str)
{
int i;
if (NULL != str)
while ('\0' != *str ) {
if (*str == 'e')
*str = 'a';
++str;
}
}
int main(int argc, char **argv){
int i;
for (i = 1; i < argc; ++i) {
replace_e_with_a(argv[i]);
puts(argv[i]);
}
}