Why does my tolower call not execute? [closed] - c

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
char * abc = "ABC";
int i;
printf("%s\n", abc);
for (i = 0; i < strlen(abc); i++)
{
abc[i] = tolower((int) abc[i]); //Error at this line
}
printf("%s\n", abc);
The line where I call tolower does not execute?

char * abc = "ABC"; defines a string literal
which cannot be changed during runtime.
Use char abc[] = "ABC";
edit: Maybe you "can" change it in some cases,
but there is no guarantee for anything.
It could work, it could crash,
or doing other strange things with your program
which are not immediately recognizable.

The abc points to a constant string, and because it is read-only, you can not change the value directly. There are 2 methods.
(1)You can allocate the memory in the stack:
char abc[] = "ABC";
int i;
printf("%s\n", abc);
for (i = 0; i < strlen(abc); i++)
{
abc[i] = tolower((int) abc[i]); //Error at this line
}
printf("%s\n", abc);
(2)You can also allocate the memory in heap, and the code is like this:
int len = strlen("ABC");
char *p = malloc(len + 1);
strcpy(p, "ABC");
printf("%s\n", p);
for (i = 0; i < len; i++)
{
p[i] = tolower((int) p[i]); //Error at this line
}
printf("%s\n", p);

Related

Splitting string sequence into integers [closed]

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.

What is the right way to explain this code? [closed]

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
}

C - Char to hexa [closed]

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 6 years ago.
Improve this question
I have str:
char *str = "lala";
Now, I would like convert any chars in str to hexadecimal, example:
str = convert(str);
print str: 0x6C 0x61 0x6C 0x61
^ l ^ a ^ l ^ a
How I can do that ?
char *convert(char const *str) {
int len = strlen(str);
char *retVal = (char *)malloc(5 * len);
char *pos = retVal;
int i;
for(i = 0; i < len; ++i, pos += 5) sprintf(pos, i? " 0x%x" : "0x%x", str[i]);
retVal[5 * len - 1] = '\0';
return retVal;
}
Might have missed something, haven't used C for eight years.
Simply by asking printf to do it :
void convert(char* str, size_t length) {
size_t i;
for(i = 0; i < length; i++)
printf("0x%02x ", str[i]);
}
You can achieve it by using this implementation....
#define MAX 100
char *convert(char *str)
{
char *hexStr = (char *)malloc(strnlen(str, MAX) * 5);
if (hexStr == NULL)
return NULL;
int i,j;
for (i=0, j=0; str[i]; j+=5, i++)
sprintf(hexStr + j, "0x%02x ", str[i]);
hexStr[--j] = '\0';
return hexStr;
}
char* x = str - 1;
while(*++x) printf("%02x ", (int) *x); // Print one character in hex
printf("\n") // Finish with a carriage return

Unknown error regarding strings and stdio.h library [closed]

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'm receiving an uknown error found in the stdio.h library.
Please can someone check it at tell me what is wrong with the code (But I thing it should work fine).
P.S. I'm new here so please don't blame me if this is a bad question.
Code:
#include <stdio.h>
#include <stdlib.h>
// Conversion from a number to a string
char *i2s(int broj);
int main()
{
char string1;
int br, n;
do
{
printf("How much numbers?\n -"), scanf("%d", &n);
} while (n < 1);
for (int i = 0; i < n; i++)
{
printf("\nbr = "), scanf("%d", &br);
string1 = *i2s(br);
printf(" %s", string1);
}
free(string1);
getch();
return 0;
}
char *i2s(int broj)
{
char *pom;
int z=0,br=0,p;
if (broj < 0)
{
z = 1;
broj = -broj;
}
p = broj;
do
{
br++;
p /= 10;
} while (p);
pom = (char *)calloc(br + 1 + z, sizeof(char));
if (z)
pom[0] = '-';
do
{
pom[--br + z] = '0' + broj % 10;
} while (broj /= 10);
return pom;
}
char string1;
free(string1);
string 1 is not a pointer.
Also with the following section you overwrite string1 everytime you run through the loop. that way you have no pointer to free() the memory that you allocate inside your function unless you do it inside the loop.
for (int i = 0; i < n; i++)
{
printf("\nbr = "), scanf("%d", &br);
string1 = *i2s(br);
printf(" %s", string1);
}

Dynamic allocation of char** [closed]

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 7 years ago.
Improve this question
This is the text of my example:
Loading number N then N words from standard input. The word is not longer than 100 characters. Dynamically allocate array of loaded words as a series of pointers to character strings (dynamic array needs to have a type char **). Provide a set of words printed in a single line with spaces between the words.
Can someone tell me how to set the character limits?
Should I do this:
scanf("%100s", str[i])
or something else?
BTW, how can I allocate the memory for a type like this (char **,int **,etc)?
This is my code that I've done, so what have I done wrong?
int main()
{
int i,n;
printf("How much words? "), scanf("%d", &n);
char *str= (char *)malloc(n*sizeof(char *));
for(i = 0; i < n; i++)
{
str[i] = malloc(100 * sizeof(char *));
printf("%d. word: ", i + 1),scanf("%s", str[i]);
}
for (i = 0; i < n; i++)
{
printf("%s ", str[i]);
}
getch();
Wrong type for array of pointers
// char *str
char **str
Code clean-up with comments.
// add void
int main(void) {
int i,n;
// Easier to understand if on 2 lines-of code
printf("How much words? ");
// test scanf() results
if (scanf("%d", &n) != 1) return -1;
// Consider different style to allocate memory, as well as type change
// char *str= (char *)malloc(n*sizeof(char *));
char **str= malloc(sizeof *str * n);
// check allocation
assert(str == NULL);
for(i = 0; i < n; i++) {
str[i] = malloc(sizeof *str[i] * 100);
// check allocation
assert(str[i] == NULL);
printf("%d. word: ", i + 1);
fflush(stdout);
// limit input width to 99
// test scanf() results
if (scanf("%99s", str[i]) != 1) return -1;
}
for (i = 0; i < n; i++) {
// Add () to clearly show beginning/end of string
printf("(%s) ", str[i]);
}
getch();
}

Resources