This question already has answers here:
Given a starting and ending indices, how can I copy part of a string in C?
(3 answers)
Closed 4 years ago.
I have a string
str = "hello"
I want to make a new string which is the first two digits of str "he".
How do I do this in C?
Use strncpy(), like this:
#include <stdio.h>
#include <string.h>
int main(void) {
char src[] = "hello";
char dest[3]; // Two characters plus the null terminator
strncpy(dest, &src[0], 2); // Copy two chars, starting from first character
dest[2] = '\0';
printf("%s\n", dest);
return 0;
}
Output:
he
Related
This question already has answers here:
Passing not null terminated string to printf results in unexpected value
(6 answers)
Printing string using printf without null character [duplicate]
(1 answer)
how to make a not null-terminated c string?
(7 answers)
'\0' and printf() in C
(9 answers)
What are the repercussions of not using null termination in strings?
(3 answers)
Closed 8 months ago.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char s[40]="Who are you tell me that I can not code?";
char c=s[8];
char st[2];
st[0]=s[0],st[1]=s[1];
printf("%s \n",st);
return 0;
}
Why is it printing my original string 's' also when I'm printing st?
Output:-
WhWho are you tell me that I can not code?►
Process returned 0 (0x0) execution time : 0.048 s
Press any key to continue.
s is not a proper C string because it has exactly 40 bytes and 40 characters. C strings must have a null terminator byte. You should define it as:
char s[] = "Who are you tell me that I can not code?";
There is the same problem for st that you define as an array of 2 bytes where you store 2 characters. You should define st as an array of 3 bytes and set a '\0' null character at st[2].
Both of these problems can cause undefined behavior. and the second does as you pass st to printf() for a %s format which expects a proper C string pointer.
Here is a modified version:
#include <stdio.h>
int main() {
char s[] = "Who are you tell me that I can not code?";
char st[3];
st[0] = s[0];
st[1] = s[1];
st[2] = '\0';
printf("%s\n", st);
return 0;
}
Note that you can also print a substring with printf using the precision field:
#include <stdio.h>
int main() {
char s[] = "Who are you tell me that I can not code?";
// print the 3 characters at offset 4
printf("%.3s\n", st + 4);
return 0;
}
This question already has answers here:
Sizeof string literal
(2 answers)
Closed 2 years ago.
I want to know the reason behind why I am getting 2 bytes as a size of the character and at the same time 1 byte for the similar character when stored in a place-holder.
#include <stdio.h>
void main(void) {
char a = "$";
printf("%d\n", sizeof("$")); // Here output is 2 which should be 1
printf("%d", sizeof(a)); // Here output is 1 which is correct
}
it treats "$" as a string, so it's NULL terminated and has a sizeof 2.
same would happen if you would do sizeof "a".
if you want to get the sizeof the character itself you should do sizeof with '$', that's the symbol for character.
This question already has answers here:
How should character arrays be used as strings?
(4 answers)
Closed 3 years ago.
I want to write a C program that stores a bunch of sentences while it's running and prints them out at the end of the program.
Here's what I wrote:
#include<string.h>
#include<stdio.h>
int main(){
int counter = 0;
char state[100][200];
char stroo[3] = "yes";
sprintf(state[counter], "%s", stroo);
counter++;
char poo[2] = "44";
sprintf(state[counter], "%s", poo);
counter++;
for (int i=0; i<counter; i++) {
printf("%s\n", state[i]);
}
return 0;
}
For the output, the first line prints "yes" which is what I expected. The second line, however, prints "44yes". Why is it printing "yes" as well as "44"?
That's because your strings aren't correctly null-terminated, so you run into undefined behavior at the sprintf. This is because the buffer is too small to hold the null-terminator. For instance here:
char stroo[3] = "yes";
You need memory for four chars, the 'y', 'e', 's' and the null-terminator. Change it to
char stroo[4] = "yes";
Likewise char poo[2] needs to be char poo[3].
See here (emphasis mine):
Successive bytes of the string literal or wide characters of the wide string literal, including the terminating null byte/character, initialize the elements of the array [...] If the size of the array is known, it may be one less than the size of the string literal, in which case the terminating null character is ignored:
If you want the size to automatically be big enough to exactly fit the string, you can just leave out the size specification:
char stroo[] = "yes";
And of course, for this example, you don't even need to copy the string to the stack:
char *stroo = "yes";
#define STRINGS 5
#define STRING_SIZE 50
char arr[STRINGS][STRING_SIZE] =
{ "Hi",
"Hello world",
"Good morning",
"ASDF",
"QWERTY"
};
for (int i=0; i<STRINGS; i++) {
printf("%s\n", arr[i]);
}
This question already has answers here:
How do I concatenate const/literal strings in C?
(17 answers)
Closed 4 years ago.
I need to concatenate a string and a variable to fill another string:
#include <stdio.h>
int main(){
char *te1;
char *te2;
char *tabela[2];
te1 = "text 1";
te2 = "text 2";
tabela[0] = "text 0, " + te1 + ", " + te2;
printf("%s\n", tabela[0]);
return 0;
}
expected result:
text 0, text 1, text 2
Ooof! You need to read up on C, pointers and arrays.
Firstly, you need to allocate some space, either using malloc and assign to a pointer or char buffer[200]
Next be aware that you cannot add strings like other languages. You need to either strcat or one of its variants or sprintf(buffer, "text 0 %s, %s",te1, te2);
There's some clues but with no ill intent meant, you really do need to ready up and understand some fundamentals.
This question already has answers here:
How do I concatenate const/literal strings in C?
(17 answers)
C appending char to char*
(6 answers)
Closed 9 years ago.
I would like to save more chars in ch[500] then it is already there. I dont want to lose the chars I've saved there before.
Something that would work like this:
ch = ch + ’nextch’;
You can use strcat() or strncat() to Concatenate two strings.
for example
char ch[100];
strcpy(ch,"hello");
strcat(ch," world");
if you want to append only one character
char str[100];
strcpy(str,"hello");
char ch='a';
char buf[2];
sprintf(buf,"%c",ch);
strncat(str,sizeof str, buf);
or
size_t length= strlen(str);
str[strlen(str)]=ch;
str[length+1]='\0';