Issues dynamically allocating strings with C [closed] - c

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 10 days ago.
The community is reviewing whether to reopen this question as of 10 days ago.
Improve this question
I know strings end with '/0' in C. While I'm dynamically allocating memory for a string, how do I handle that?
Also, how would you print out a dynamically allocated string? Because I've tried the regular way to print out a string and it did not work.
For the first question, I tried:
int len;
scanf("%d", &len);
char* str = (char*)malloc(sizeof(char)*len);
for (int i = 0; i<len; ++i){
scanf("%c", (str+i));
}
For the second question, I tried
printf("%s", *str);

Related

assign zero to item in char array [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 9 months ago.
Improve this question
I have some materials showing this code. what does code means by making these assignments?
char inputfilename[128];
inputfilename[0] = 0;
char *argv[128];
*argv[1] = 0;
In C, character arrays are terminated by a null character (value 0). In both cases in your example, the code initializes the strings to "empty" (with a terminator in the first element). This would prove useful in any subsequent string operations (strcat, strcpy, etc.).

How does the code that prints array elements works? [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 3 years ago.
Improve this question
This code prints the array elements, but I can't understand how does k[x-1] gives the array elements.
#include<stdio.h>
int main()
{
int x[]={2,4,6,8,10},k=1;
while (k<=5)
{
printf ("%3d",k[x-1]);
k++;
}
return 0;
}
Array indexes start at 0 in C. An array like int x[]={2,4,6,8,10} will have a value x[0]=2 and so forth. Typically, when iterating through an array, a convention like this is used:
for (int i = 0; i < length; i++)
printf("%3d",x[i]);
Since the code you provided begins the indexing at 1, you have to subtract one to fetch the proper element.

How to copy all chars(include '\0') from one array to another without using strcpy function? [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 3 years ago.
Improve this question
copy all characters from one character array to another without using strcpy function.
char s1[80],s2[80];
int i;
printf("input s2");
scanf("%s",&s2);
for(i=0;i<=strlen(s2);i++)
s1[i]=s2[i];
printf("s1:%s",s1);
Instead of scanf, using gets function for getting input with spaces.
#include<stdio.h>
int main()
{
char s1[80],s2[80];
int i;
printf("input s2");
// scanf("%c",&s2);
gets(s2);
printf("%d",strlen(s2));
for(i=0;i<strlen(s2);i++)
{
s1[i]=s2[i];
}
printf("s1:%s",s1);
}

How to split a string into 3 parts in C [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 5 years ago.
Improve this question
the question is the following: "How do I split a string into 3 parts in C?"
The string is something similar to the following: "Roberta$$$Anna$$$$$$Massimo$$$"
I need to split it exactly after 10 characters (Roberta$$$, Anna$$$$$$, Massimo$$$), and please notice that they aren't separated by a spacebar, so I think I cannot use the strtok function or the library string.h to split them.
char source[] = "Roberta$$$Anna$$$$$$Massimo$$$";
char part1[11];
char part2[11];
char part3[11];
memmove(part1, &source[ 0], 10);
part1[10] = '\0';
memmove(part2, &source[10], 10);
part2[10] = '\0';
memmove(part3, &source[20], 10);
part3[10] = '\0';
You could use strncpy() or memcpy() instead of memmove().

How to extract specified character string from string [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 9 years ago.
Improve this question
char string[]="DGS021J0W0S1000.0S20000S3000.0S4000.0S50.00S60.00F";
how to get S[1-5]
thanks!
Use strncpy() standard function
char S[6] = {0};
strncpy(S, string+1 , 5);
If you want to copy from the beginning of the string to the 5th charachter, then your question should be
how to get S[0-4]
and not S[1-5] because array index in C start from 0 and not from 1. and the solution for this case will be
char S[6] = {0};
strncpy(S, string , 5);
I think you are looking for substring methods.
You can do it in two for loops in C.

Resources