This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 6 years ago.
This is what I'm trying to do but my code is either not compiling or giving me an unexpected output "BC" instead of just "B".
#include <stdio.h>
void removeFirstAndLastChar(char** string) {
*string += 1; // Removes the first character
int i = 0;
for (; *string[i] != '\0'; i++);
*string[i - 1] = '\0';
}
int main(void) {
char* title = "ABC";
removeFirstAndLastChar(&title);
printf("%s", title);
// Expected output: B
return 0;
}
I looked through a lot of answers here related to passing pointers by reference but none of them seemed to contain the operations that I want to do in my removeFirstAndLastChar() function.
I do not judge your algorithm or C conventions, friends who comment on your problem are totally right. But if you still do it in this way you can use this approach.
#include <stdio.h>
#include <string.h>
void removeFirstAndLastChar(char* string) {
memmove(string,string+1,strlen(string));
string[strlen(string)-1]=0;
}
int main(void) {
char title[] = "ABC";
removeFirstAndLastChar(title);
printf("%s", title);
// Expected output: B
return 0;
}
Related
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 3 years ago.
#include <stdio.h>
#include <string.h>
void space_to_tab(char *string) {
char str [strlen(string)];
int size = 0;
while(string[size] != '\0'){
if(string[size] == ' ')
str[size] = '\t';
else
str[size] = string[size];
size++;
}
*string = *str;
}
int main() {
char *str = "aa b";
printf("%s\n", str);
space_to_tab(str);
printf("%s\n", str);
}
I just started with C programming and I want to switch the spaces in a string with tabs and I get the error "Segmentation fault (core dumped)".
I belive the error is in "*string = *str;" but I dont know how to change the pointer of one string to the other.
You should not modify literal strings (i.e. "aa b") as this results in undefined behaviour (hence the segmentation fault). Instead you should modify an array like such:
char str[] = "aa b";
See In C, can I initialize a string in a pointer declaration the same way I can initialize a string in a char array declaration?
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Why is this string reversal C code causing a segmentation fault? [duplicate]
(8 answers)
Closed 4 years ago.
I'm attempting to remove duplicate characters from a string without using any additional buffer. The code works when I declare a single variable like this
char s[] = "aaabbb";
but not when I'm attempting to loop through a few test cases.
#include <stdio.h>
#include <string.h>
/* Design an algorithm and write code to remove the duplicate characters in a string
without using any additional buffer. NOTE: One or two additional variables are fine.
An extra copy of the array is not. */
void removeDuplicates(char s[]) {
// attempts to modify array in place without extra buffer
}
int main() {
char *s[4] = {"aaaa", "abcd", "ababab", "aaabbb"};
int i;
for (i = 0; i < 6; i++) {
removeDuplicates(s[i]);
}
return 0;
}
This returns Bus error: 10 because it's attempting to modify the string literal "aaaa" but I'm not sure how to overcome this while maintaining a nice setup for the test cases.
The s[i] are pointing to string literals, you need a 2-dimensinal char array like this:
char s[][7] = {"aaaa", "abcd", "ababab", "aaabbb"}
Also note that for a string of length n you need at least n+1 spaces because of the '\0'-termination."aaabbb"` has length 6, so it need at least 7 spaces.
Then you can do
int main() {
char s[][7] = {"aaaa", "abcd", "ababab", "aaabbb"};
size_t i;
for (i = 0; i < sizeof s / sizeof s[0]; i++) {
removeDuplicates(s[i]);
}
return 0;
}
This question already has answers here:
why this program doesn't give the expected output? [duplicate]
(2 answers)
Closed 7 years ago.
I'm stuck here trying to understand why this assignement can't work in this way in C. What I'm trying to do is substitute all space occurrences with underscore char. (output: Hi_from_Synchronyze)
I saw that the problem comes when I try to do this..
s[n]='_';
the complete code is this one
#include <stdio.h>
#include <stdlib.h>
char *underscore(char *s, int n);
int main()
{
printf("%s", underscore("Hi from Synchronyze", 0));
return 0;
}
char *underscore(char *s, int n)
{
if(s[n]=='\0')
return s;
else {
if(s[n]==' ') {
s[n]='_';
return underscore(s, n+1);
}
else return underscore(s, n+1);
}
}
I'd like to know what's going on behinde and why this happens, not the solution.
Thank you very much in advance
String literals are read-only, so you can't assign to them.
Make a mutable copy of the string first, something like:
char text[] = "Hi from Synchronyze";
printf("%s", underscore(text, 0));
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 7 years ago.
I am trying to write a simple encryption program (Caesar cipher) and I am running into a snag. I am relatively new to the world of C and pointers, coming from Java originally.
Whenever I run the following code, it will give me the error message Segmentation fault and terminate.
I have done a little reading about what that means, but I still don't fully understand it, or what is wrong with my code, or how to remedy the issue.
If you could help with any of those things that would be greatly appreciated.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void encrypt(char *input);
int main()
{
char *instructions = "pipi";
encrypt(instructions);
printf("Here are your secret instructions:\n%s\n", instructions);
return (0);
}
void encrypt(char *input) {
while (*input != '\0') {
if (isalpha(*input)) {
*input += 1;
if (!isalpha(*input)) {
*input -= 26;
}
}
input++;
}
}
String literals in C, like "pipi" are read only, trying to modify such a string will lead to undefined behavior.
Use an array if you want to modify the string:
char instructions[] = "pipi";
This question already has answers here:
Modifying String Literal [duplicate]
(4 answers)
Closed 10 years ago.
I am trying to remove two consecutive duplicate elements from the string.I am getting segmentation fault in the line 16 .even the commented 17th line also get me the same error.
dont worry about my logic of my program.it may be wrong...but i am struck with this error..help me out...explain why i am getting this error
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *st="fvvbbyyr";
int i=0,j,len;
for(len=0;st[len]!='\0';len++);
for(i=0;i<len;i++)
{
if(st[i]==st[i+1])
{
for(j=i+2;j<len;j++)
{
*(st+j-2)=*(st+j);
//st[j-2]=st[j];
}
len = len-2;
i=-1;
}
}
return 0;
}
char *st = "fvvbbyyr";
st points to the anonymous string "fvvbbyyr", which can be located in read-only memory.
*(st+j-2) = *(st+j);
Attempting to modify such value leads to an undefined behavior.
A good habit is to declare the pointer as const char * because a string litteral behaves in this way.
const char *st = "fvvbbyyr";
Then your compiler should print some warnings/errors. Use rather an array:
char st[] = "fvvbbyyr";
BTW, the array subscripting operator [] may make your code more readable.
st[j - 2] = st[j];
Moreover you are accessing to st[i+1] == st[len] once in your loop.