I'm trying to append characters from a string into a new string. For the code below:
int main(void)
{
char s1[] = "cat";
char s2[] = "hsklsdksdhkjadsfhjkld";
strcat(s1, &s2[1]);
printf("s1 is now %s\n", s1);
}
Why is the output catsklsdksdhkjadsfhjkld and not cats? Why is the whole string added, instead of just the 's' located at s2[1]?
Thanks.
since a char * is only a pointer to the start of a string; C supposes the end of the string is a \0 character. So all characters are added until he meets the \0 character
you suppose &s2[1] points to "s", (which is true), but since it is a char pointer, it points to the whole char array, until the \0 character at the end. Try this for example:
printf("%s\n", &s2[1]);
which will yield:
sklsdksdhkjadsfhjkld
from the reference of strcat:
Concatenate strings
Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.
EDIT
if you want to add only one (or some) characters, use strncat:
strncat(s1, &s2[1], 1 /*number of chars to append*/);
EDIT2
make sure your char arrays are large enough, as suggested by #PaulR:
char s1[32] = "cat";
Both arguments to strcat are pointers to char objects, and both are assumed to point to (the initial character of) a string.
For example, this:
strcat(s1, &s2[0]);
is equivalent to this:
strcat(s1, s2);
In both cases, the second argument is a pointer to the initial character of a string, which strcat uses to access the entire string up to and including the terminating '\0' null character.
But your program has undefined behavior. By declaring
char s1[] = "cat";
you let the compiler determine the size of s1 based on the initializer. In this case, s1 is an array of 4 chars (including the terminating '\0'). There is no room to append anything to it. Apparently when you ran it, it copied characters into the memory space immediately following s1, which is why it seemed to "work".
A working version of your program is:
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[50] = "cat";
char s2[] = "hsklsdksdhkjadsfhjkld";
strcat(s1, &s2[1]);
printf("s1 is now %s\n", s1);
}
Note the #include directives. These are not optional, even though you might get away with omitting them.
You can use strncat instead of strcat:
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[32] = "cat"; // NB: s1 needs to be large enough to hold additional chars !
char s2[32] = "hsklsdksdhkjadsfhjkld";
strncat(s1, &s2[1], 1);
printf("s1 is now %s\n", s1);
return 0;
}
LIVE DEMO
Because strcat() takes two string arguments, and because &s2[1] isn't a character, it's the string "sklsdksdhkjadsfhjkld";
So strcat(s1, &s2[1]); concatenates
"cat" + "sklsdksdhkjadsfhjkld"
Giving the result
"catsklsdksdhkjadsfhjkld"
If you want to append a single character you could do this
len = strlen (s1);
s1[len] = s2[1];
s1[len+1] = '\0'; // new string terminator
but you would have to ensure there is enough array (string) space available.
Related
I was solving a challenge on CodeSignal in C. Even though the correct libraries where included, I couldn't use the strrev function in the IDE, so I looked up a similar solution and modified it to work. This is good. However, I don't understand the distinction between a literal string and an array. Reading all this online has left me a bit confused. If C stores all strings as an array with each character terminated by \0 (null terminated), how can there be any such thing as a literal string? Also if it is the case that strings are stored as an array, *would inputString store the address of the array or is it an array itself of all the individual characters stored.
Thanks in advance for any clarification provided!
Here is the original challenge, C:
Given the string, check if it is a palindrome.
bool solution(char * inputString) {
// The input will be character array type, storing a single character each terminated by \0 at each index
// * inputString is a pointer that stores the memory address of inputString. The memory address points to the user inputted string
// bonus: inputString is an array object starting at index 0
// The solution function is set up as a Boolean type ("1" is TRUE and the default "0" is FALSE)
int begin;
// The first element of the inputString array is at position 0, so is the 'counter'
int end = strlen(inputString) - 1;
// The last element is the length of the string minus 1 since the counter starts at 0 (not 1) by convention
while (end > begin) {
if (inputString[begin++] != inputString[end--]) {
return 0;
}
} return 1;
}
A string is also an array of symbols. I think that what you don't understand is the difference between a char pointer and a string. Let me explain in an example:
Imagine I have the following:
char str[20]="helloword";
str is the address of the first symbol of the array. In this case str is the address of h. Now try to printf the following:
printf("%c",str[0]);
You can see that it has printed the element of the addres that is 'h'.
If now I declare a char pointer, it will be poining to whatever char adress I want:
char *c_pointer = str+1;
Now print the element of c_pointer:
printf("%c",c_pointer[0]);
You can see that it will print 'e' as it is the element of the second adress of the original string str.
In addition, what printf("%s", string) does is to printf every elemet/symbol/char from the starting adress(string) to the end adress where its element is '\0'.
The linked question/answers in the comments pretty much cover this, but saying the same thing a slightly different way helps sometimes.
A string literal is a quoted string assigned to a char pointer. It is considered read only. That is, any attempts to modify it result in undefined behavior. I believe that most implementations put string literals in read-only memory. IMO, it's a shortcoming of C (fixed in C++) that a const char* type isn't required for assigning a string literal. Consider:
int main(void)
{
char* str = "hello";
}
str is a string literal. If you try to modify this like:
#include <string.h>
...
str[2] = 'f'; // BAD, undefined behavior
strcpy(str, "foo"); // BAD, undefined behavior
you're broken the rules. String literals are read only. In fact, you should get in the habit of assigning them to const char* types so the compiler can warn you if you try to do something stupid:
const char* str = "hello"; // now you should get some compiler help if you
// ever try to write to str
In memory, the string "hello" resides somewhere in memory, and str points to it:
str
|
|
+-------------------> "hello"
If you assign a string to an array, things are different:
int main(void)
{
char str2[] = "hello";
}
str2 is not read only, you are free to modify it as you want. Just take care not to exceed the buffer size:
#include <string.h>
...
str2[2] = 'f'; // this is OK
strcpy(str2, "foo"); // this is OK
strcpy(str2, "longer than hello"); // this is _not_ OK, we've overflowed the buffer
In memory, str2 is an array
str2 = { 'h', 'e', 'l', 'l', '0', '\0' }
and is present right there in automatic storage. It doesn't point to some string elsewhere in memory.
In most cases, str2 can be used as a char* because in C, in most contexts, an array will decay to a pointer to it's first element. So, you can pass str2 to a function with a char* argument. One instance where this is not true is with sizeof:
sizeof(str) // this is the size of pointer (either 4 or 8 depending on your
// architecture). If _does not matter_ how long the string that
// str points to is
sizeof(str2) // this is 6, string length plus the NUL terminator.
I have read the documentation of strcat() C library function on a few websites.
I have also read here: Does strcat() overwrite or move the null?
However, one question is still left - can strcat() function be used to override the characters in the destionation string (assume that dest string has enough space for the source string, so there will be no errors)?
I ran the following code and found that it doesn't have the ability to override the dest string's characters...
char dest[20] = "Hello World";
char src[] = "char";
strcat(dest+1, src);
printf("dest: %s", dest);
Assume that the goal is to have a destination string that contains: "Hchar World!"
(I know that strcat() also copies the NULL characters('\0') to the dest string, so if printf() function is called, it should print Hchar, as I mistakenly thought would happen...).
Is that a possible task to do with strcat()? If not, is strcpy() the answer to the question?
If there is an assignment of '\0' (NULL character) in the middle of the string, for example, will strcat() always treat the first '\0' (NULL character) it meets? I mean, If I had:
char str[] = "Hello";
str[2]= 0;
strcat(str, "ab");
I just want to be sure and clarify the misunderstanding. I will be glad to read explanations.
As noted in the comments, the strcat function will always (attempt to) append the string given as its second argument (traditionally called src) to that given as its first (dest); it will produce undefined behaviour if either string is not null-terminated or if the destination buffer is not large enough.
The cppreference site gives better documentation (for both C and C++) than the website you linked. From that site's strcat page:
(1) … The character src[0] replaces the null terminator at the
end of dest. The resulting byte string is null-terminated.
And:
Notes
Because strcat needs to seek to the end of dest on each call, it is inefficient to concatenate many strings into one using strcat.
So, in the code you show, calling strcat(dest+1, src); has the same effect as calling strcat(dest, src);. However, calling strcpy(dest+1, src); will produce the result you want (printing Hchar).
strcat will write src string at the end of dst.
If you want to override dst with strcat, you first need to make dst "end" where you want to override it.
Take a look at this code sample:
#include <stdio.h>
#include <string.h>
int main()
{
char dst[20] = "Hello world";
char src[] = "char";
dst[1] = '\0';
strcat(dst, src);
printf("%s\n", dst);
return (0);
}
However, this is not the aim of strcat, and as said in the comments, the use of strcpy would be more appropriate here.
#include <stdio.h>
#include <string.h>
int main()
{
char dst[20] = "Hello world";
char src[] = "char";
strcpy(dst + 1, src);
printf("%s\n", dst);
return (0);
}
I am appending a string using single character, but I am not able to get it right. I am not sure where I am making mistake. Thank you for your help in advance. The original application of the method is in getting dynamic input from user.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(){
int j;
char ipch=' ';
char intext[30]="What is the problem";
char ipstr[30]="";
printf("Input char: ");
j=0;
while(ipch!='\0'){
//ipch = getchar();
ipch = intext[j];
printf("%c", ipch);
strcat(ipstr,&ipch);
j++;
}
puts("\n");
puts(ipstr);
return;
}
Following is the output I am getting.
$ ./a.out
Input char: What is the problem
What is h e
p
oblem
change
strcat(ipstr,&ipch);
to
strncat(ipstr, &ipch, 1);
this will force appending only one byte from ipch. strcat() will continue appending some bytes, since there's no null termination character after the char you are appending. as others said, strcat might find somewhere in memory \0 and then terminate, but if not, it can result in segfault.
from manpage:
char *strncat(char *dest, const char *src, size_t n);
The strncat() function is similar to strcat(), except that
it will use at most n characters from src; and
src does not need to be null-terminated if it contains n or more characters.
strcat requires its second argument to be a pointer to a well-formed string. &ipch does not point to a well-formed string (the character sequence of one it points to lacks a terminal null character).
You could use char ipch[2]=" "; to declare ipch. In this case also use:
strcat(ipstr,ipch); to append the character to ipstr.
ipch[0] = intext[j]; to change the character to append.
What happens when you pass &ipch to strcat in your original program is that the function strcat assumes that the string continues, and reads the next bytes in memory. A segmentation fault can result, but it can also happen that strcat reads a few garbage characters and then accidentally finds a null character.
strcat() is to concatenate strings... so passing just a char pointer is not enough... you have to put that character followed by a '\0' char, and then pass the pointer of that thing. As in
/* you must have enough space in string to concatenate things */
char string[100] = "What is the problem";
char *s = "?"; /* a proper '\0' terminated string */
strcat(string, s);
printf("%s\n", string);
strcat function is used to concatenate two strings. Not a string and a character. Syntax-
char *strcat(char *dest, const char *src);
so you need to pass two strings to strcat function.
In your program
strcat(ipstr,&ipch);
it is not a valid statement. The second argument ipch is a char. you should not do that. It results in Segmentation Fault.
I tried to code a function which replace all string s1 to s2, in a given string s.
however, i don't know why my program stop at the line *p=0 in that replace function without any error reported? ##
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void replace(char * s, char * s1, char * s2) {
char * p; int l=strlen(s2);
while ((p=strstr(s,s1))) {
*p=0;
p+=l;
strcat(s,s2);
strcat(s,p);
}
}
int main(void) {
char *s=(char *)"cmd=ls+-la&abc=xyz";
replace (s, "+", " ");
printf("%s", s);
return EXIT_SUCCESS;
}
There are some problems with the replace function but, first of all, there is a big difference between a pointer to a constant char array vs a character array:
char *str = "some string";
Assigns str the address of the immutable character array (read-only), it does not copy the string, only pointers are involved. Any attempt to modify that string will result in undefined behavior.
char str[] = "some string";
In this case str is an array (of size big enough to hold the string + \0) that is initialized to that string, allowing the modification of individual characters within the array.
Back to your replace function.
I will start with the first thing that I saw which is your use of strstr and strcat inside the loop is highly inefficient. Every time you call strstr it starts from the beginning of the string and searches for the first occurrence of the second string all over, the same problem can be seen with strcat which needs to find the null-terminator every time.
Another issue I see is if the replacement string (s2) is longer than the original string (s1) you must shift the entire string to accommodate for the additional characters of the new string. The same issue will occur if the replacement string is shorter.
a basic method to replace a simple char might look like this:
while (*s)
{
if (*s == c1)
*s = c2;
++s;
}
a little more complex method to replace a string would be:
/* PRECONDITION: strlen(s1) == strlen(s2) */
int l = strlen(s2);
while (*s)
{
if (!strncmp(s, s1, l))
{
memcpy(s, s2, l);
s += l;
}
else
++s;
}
Your compiler is allowed to place string literals into read-only memory, which is probably what it did with s.
Try:
char s[] = "cmd=ls+-la&abc=xyz";
This changes s from a pointer to a string literal into an array initialized with your string.
Using GDB, I find I get a segmentation fault when I attempt this operation:
strcat(string,¤tChar);
Given that string is initialized as
char * string = "";
and currentChar is
char currentChar = 'B';
Why does this result in a segmentation fault?
If strcat can't be used for this, how else can I concat a char onto a string?
As responded by others, ¤tChar is a pointer to char or char*, but a string in C is char[] or const char*.
One way to use strcat to concatenate a char to string is creating a minimum string and use it to transform a char into string.
Example:
Making a simple string, with only 1 character and the suffix '\0';
char cToStr[2];
cToStr[1] = '\0';
Applying to your question:
char * string = "";
char currentChar = 'B';
cToStr will assume the string "B":
cToStr[0] = currentChar;
And strcat will work!
strcat ( string, cToStr );
Because ¤tChar is not a string, it doesn't finish with \0 character. You should define B as char *currentChar = 'B';. Also according to http://www.cplusplus.com/reference/clibrary/cstring/strcat string should have enough space to hold the result string (2 bytes in this case), but it is only 1 byte.
Or if you want to use char then you can do something like (depending of your code):
char string[256];
...
char currentChar = 'B';
size_t cur_len = strlen(string);
if(cur_len < 254) {
string[cur_len] = currentChar;
string[cur_len+1] = '\0';
}
else
printf("Not enough space");
I think the simplest method (not efficient) would be sprintf
sprintf(str, "%s%c", str, chr);
strcat() takes two '\0'-terminated strings. When you pass the address of a character, the routine will look at the memory that follows the character, looking for the terminator.
Since you don't know what that memory even refers to, you should expect problems when your code accesses it.
In addition to that, your string argument does not have room to have any characters appended to it. Where is that memory written to? It will attempt to write past the end of the memory associated with this string.
Both of the strings must be null-terminated. A single char isn't null terminated, so it's undefined when strcat will stop concatenating characters to the end. Also, string must contain at least enough space for both the original string and resultant string.
This works:
char string[10] = "";
char* currentChar = "B";
strcat(string, currentChar);
We know that currentChar = 'B'.
This can be done
strcat(string, "B\0");
If we know currentChar will be hardcoded as 'B', this would be a good approach.
It also removes the need for char currentChar = 'B';
The first argument of strcat must have enough space to hold the rest of the string. "" is a constant string and as such GCC does not allocate space.
Make it an array with enough space:
char buf[1024];
strcat(buf, "");
strcat(buf, "B");