strcat is giving me a segmentation fault error - c

After verifying that strcat is where the error occurs, I then check the previous example in my assignment. In my previous examples I use strcat(actually strncat) in the same fashion as I do for my following code. I am not too sure.
The purpose of my program is to loop through "string" and remove any occurances the character 'c' from string.
main.c:
char string[100]={0}, c[3];
printf("Enter a String: ");
fgets(string, 100, stdin);
if (string[98] == '\n' && string[99] == '\0') { while ( (ch = fgetc(stdin)) != EOF && ch != '\n'); }
printf("Enter a Char: ");
fgets(c, 2, stdin);
while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
rmchr(string, c[0]);
header:
rmchr(char *string, char c)
{
int i=0;
char *word[100];
int s = strlen(string);
for(i=0; i<=(s-2); i++)
{
if(string[i] != c)
{
strcat(word, string[i]);
}
}
}

char *word[100];
It will hold a string in your program so use:
char word[100];
that is, an array of char instead of an array of char *.
Then strcat concatenates to a string but word is not initialized. Make it a string with:
word[0] = '\0';
Then string[i] is a character but strcat needs pointers to character arguments: to use a pointer use &string[i].
Finally the problem in your rmchr function is it has to return something, either through the arguments or via a return statement but it doesn't.

There are more than one point to mention here, like
rmchr() definition should have a return type, maybe void if you're not returning anything.
[FWIW, In that case, I wounder, how you'll make use of the local variable word]
inside rmchr(), word needs to be an array of chars, not char pointers. You need to change char * word[100] to char word[100].
In strcat(), both the arguments, needs to be a pointer. You need to use &string[i], in that case.

The following seems to compile fine but your code doesnt do quite what you said you wanted, "The purpose of my program is to loop through "string" and remove any occurances the character 'c' from string.". the function doesn't remove the character or return a copy of the string with the character excluded. I wrote a function that copies the string after removing the character and returns pointer to it. below is your code a bit modified and under it is my function
//Just a compilable version of your code, not sure if it does what u want
#include <stdio.h>
#include <string.h>
void rmchr(char *string, char c)
{
int i=0;
char word[100];
int s = (int)strlen(string);
for(i=0; i<=(s-2); i++)
{
if(string[i] != c)
{
strcat(word, (char *)(&string[i]));
}
}
}
int main(int argc, const char * argv[]) {
char string[100] = {0}, c[3];
char ch;
printf("Enter a String: ");
fgets(string, 100, stdin);
if (string[98] == '\n' && string[99] == '\0') {
while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
}
printf("Enter a Char: ");
fgets(c, 2, stdin);
while ( (ch = fgetc(stdin)) != EOF && ch != '\n');
rmchr(string, c[0]);
return 0;
}
There you go, with a demo main
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* rmchr(char *string, char ch)
{
int counter = 0;
int new_size = 0;
char corrected_string[100];
while (string[counter] != '\n' && string[counter] != '\0' && string[counter] != EOF) {
if (string[counter] != ch) {
corrected_string[new_size] = string[counter];
new_size++;
}
counter++;
}
char *new_string = (char *)malloc((new_size+1) * sizeof(char));
for (int j = 0; j <= new_size; j++) {
new_string[j] = corrected_string[j];
}
return new_string;
}
int main(int argc, const char * argv[]) {
char *s = "The char 'c' will be removed";
char *new = rmchr(s, 'c');
printf("%s", new);
return 0;
}

Related

pointer with equality operator ( problem! )

*why equality operator with the pointer on function char (checker ) has no effect on actual string
#define MAXLINE 20
char strings[MAXLINE];
char nl[]= "\n";
char space[]= " ";
char tab[]= "\t";
char *tab_p=tab;
int i=0,j=0,c;
char *s;
//char* t;
char *checker(char *cp);//function checks for \n,\s,\t
int main()
{
while ( (c=getchar() )!= EOF )
strings[i++]=c;
s=checker(strings);//s gets address return value
while(*(s+j) != '\0'){
printf("%c",*(s+j));
j++;
}
}
char *checker( char *cp)
{
while( *(cp+i) != '\0'){
if(*(cp+i) == *tab_p) /* this condition is not applied why?*/
*(cp+i)= '\b';
i++;
}
return cp;
}
** I am trying to replace the tab in the original string with backspace and return the modified string but the equality operator have no effect on the original string .**
could anyone help me through this.
This is caused in part by abusing global variables.
When you enter checker, the value of the global i is already set to the length of the string. So the while loop is not entered.
Move all of the global variables into the functions where they're needed. Specifically, move them all to main and also add i to checker.
#define MAXLINE 20
char *checker(char *cp);
int main()
{
char strings[MAXLINE] = {0};
int i=0,j=0,c;
char *s;
while ( (c=getchar() )!= EOF )
strings[i++]=c;
s=checker(strings);
while(*(s+j) != '\0'){
printf("%c",*(s+j));
j++;
}
}
char *checker( char *cp)
{
char tab = '\t';
int i=0;
while( *(cp+i) != '\0'){
if(*(cp+i) == tab)
*(cp+i)= '\b';
i++;
}
return cp;
}

Read a line using Pointer variable in C

[Code]
#include <stdio.h>
//#include <conio.h> // getch()
int main(int argc, char *argv[])
{
char c, *s;
printf("Enter the number...\n");
while ((c=getchar()) != EOF && c != '\n' && c != '\t') {
putchar(c);
*s++ = c; // **What is wrong in here, the code is crashing!!**
}
printf("The number: %s\n", s);
return 0;
}
Output:
c:\works\workout\c>gcc tmp.c -o tmp
c:\works\workout\c>tmp
Enter the number...
434232
4
c:\works\workout\c>
Expected Output:
The actual given input number/string! (e.g 434232 in here)
Expecting output by using 'pointer' only (Without using 'scanf', 'char s[10]' etc.)
Thanks in advance!
First, you have to allocate s because it is a pointer.
Secondly, declare int c instead of char c because the definition of the functions getchar() and putchar() as:
int putchar(int c);
int getchar(void);
Finally, Use s[i] = ... instead of *s++ =.
The complete code (In this code, i use the realloc function for allocating each time you get new value from keyboard):
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int c;
char *s = malloc(sizeof(char) + 1);
if (s == NULL)
exit(-1);
printf("Enter the number...\n");
int i = 0;
while ((c=getchar()) != EOF && c != '\n' && c != '\t') {
putchar(c);
s[i] = c;
// re-allocate because the size of s has to increase to store the new value
s = realloc(s, sizeof(char));
if (s == NULL)
exit(-1);
i++;
}
printf("The number: %s\n", s);
return 0;
}

reading and saving known number of lines of unknown lengths of strings

I want to receive the number of lines of input from the user then read and save the lines of unknown lengths in an array.
I know that the way I am saving the lines is wrong but I don't know how to correct it.
int nos; // number of strings
scanf_s("%d", &nos);
char** strs = malloc(nos * sizeof(char*)); // array of strings
for (int i = 0; i < nos; i++) // receiving strings
{
scanf_s("%s", (strs+i));
}
You're close, but you're forgetting to allocate memory for the string. If you're working with POSIX-compliant systems (i.e. pretty much everything except Windows) then use the %ms scanf() format specifier to allocate the buffer for the string as you're reading it (note that this stops after whitespace):
scanf("%ms", &strs[i]);
For Windows, implement a gets()-like function:
#include <stdlib.h>
#include <stdio.h>
int msgets(char **str)
{
int ch = 0;
size_t len = 0;
while(ch != '\n')
{
len++;
*str = realloc(*str, len);
ch = getchar();
(*str)[len-1] = ch;
}
(*str)[--len] = 0;
return len;
}
Here's how to use it in replacement of the scanf() line:
msgets(&strs[i]);
Other than that, your code looks fine.
Here's an almost complete example with my code included:
#include <stdlib.h>
#include <stdio.h>
int msgets(char **str)
{
int ch = 0;
size_t len = 0;
while(ch != '\n')
{
len++;
*str = realloc(*str, len);
ch = getchar();
(*str)[len-1] = ch;
}
(*str)[--len] = 0;
return len;
}
int main(void)
{
int nos; // number of strings
scanf("%d ", &nos);
char** strs = malloc(nos * sizeof(char*)); // array of strings
for (int i = 0; i < nos; i++) // receiving strings
{
msgets(&strs[i]);
}
/* Do something with strs[] here */
return 0;
}
if you read carefully this answerHow can I read an input string of unknown length? , and modify your code it should be something like this.
I also add a print for loop to see the results of this code
#include <stdio.h>
#include <stdlib.h>
char *inputString(FILE* fp, size_t size){
char *str=NULL;
int ch;
size_t len = 0;
str = realloc(str, sizeof(char)*size);
if(!str){
printf("[DEBUG]\n");
return str;
}
while(EOF!=(ch=fgetc(fp)) && ch != '\n'){
str[len++]=ch;
if(len==size){
str = realloc(str, sizeof(char)*(size+=16));
if(!str)return str;
}
}
str[len++]='\0';
return realloc(str, sizeof(char)*len);
}
void empty_stdin (void) /* simple helper-function to empty stdin */
{
char c;
while ((c = getchar()) != '\n' && c != EOF);
return;
}
int main(void){
int nos,i; /*number of strings*/
scanf("%d", &nos);
empty_stdin();
char ** strs = malloc(nos * sizeof(char*)); /*array of strings*/
for (i = 0; i < nos; i++) {/*receiving strings*/
*(strs+i) = inputString(stdin,1);
}
for(i=0;i<nos;i++){
printf("%s\n",*(strs+i));
}
return 0;
}
input:
3
123456789
foo
hello world
output:
123456789
foo
hello world

Reading Multiple lines in C

So I am trying to read input from a text file and print the exact same thing I read in C.So this below is the input followed by enter:
input: Hi
output: Hi
#include <stdio.h>
#include <stdlib.h>
char *inputString(FILE *fp, size_t size) {
//The size is extended by the input with the value of the provisional
char *str;
int ch;
size_t len = 0;
str = realloc(NULL, sizeof(char) * size); //size is start size
if (!str)
return str;
while (EOF != (ch = fgetc(fp)) && ch != '\n') {
str[len++] = ch;
if (len == size) {
str = realloc(str, sizeof(char) * (size += 16));
if (!str)
return str;
}
}
str[len++] = '\0';
return realloc(str, sizeof(char) * len);
}
int main(void) {
char *m;
// printf("input string : ");
m = inputString(stdin, 10);
printf("%s\n", m);
free(m);
return 0;
}
For this input:
Hi, this is the first line
This is the second line
This is the third line \n
This is the output I expected:
Hi, this is the first line
This is the second line
This is the third line \n
This is what I got:
Hi, this is the first line
It makes sense that the code is printing only the first line, but since the condition in the guard will no longer be true after hitting the new line, but I don't know how to structure my code so it reads line by line and prints them respectively.
If you want the code to read each line, remove && ch != '\n' from the condition of the while loop.
Also, the code is reading from stdin instead of a file. Use fopen to read from a file, i.e. m = inputString(fopen("filename.txt", "r"), 512).
Try this,
#include<stdio.h>
void main(int argc, char **argv)
{
int cnt=0;
char buf[1024];
FILE *fptr=stdin;
printf("Input: \n");
char ch=fgetc(fptr);
buf[cnt++]=ch;
while(ch!='$')
{
buf[cnt++]=ch;
ch=fgetc(fptr);
}
buf[cnt++]='$';
buf[cnt]='\0';
printf("Output:\n");
fputs(buf,stdout);
fclose(fptr);
}
I have put '$' as the delimiter.
I have used an extra buffer as newline is bound to EOF for stdin. So if I print out the character immediately it comes out of loop.
All you need is repeat the process as long as you can read lines:
int main(void) {
char *m;
// printf("input strings: ");
while ((m = inputString(stdin, 10)) != NULL) {
printf("%s\n", m);
free(m);
}
return 0;
}
For this to work correctly, you must return NULL at end of file:
#include <stdio.h>
#include <stdlib.h>
char *inputString(FILE *fp, size_t size) {
//The size is extended by the input with the value of the provisional
int ch;
size_t len = 0;
char *str = malloc(size);
if (str == NULL)
return NULL;
while ((ch = fgetc(fp)) != EOF && c != '\n') {
if (len + 2 > size) {
char *new_str = realloc(str, size += 16);
if (!new_str) {
free(str);
return NULL;
str = new_str;
}
str[len++] = ch;
}
if (c == EOF && len == 0) {
/* at end of file */
free(str);
return NULL;
}
str[len++] = '\0';
return realloc(str, len);
}
Instead of:
while(EOF!=(ch=fgetc(fp))&& ch != '\n' ){
// stuff
}
you could do:
while(EOF!=(ch=fgetc(fp))){
// stuff
if (ch == '\n') break;
}
Now you have consumed the newline.

Multiple line input string using fgets

I want to input a multiple-line string using:
fgets(str,100,stdin)
and then output the same string.
For example:
Input:
my name is sandy
i am learning C
and the output should be:
my name is sandy
i am learning C
#include <stdio.h>
#include <string.h>
char *mfgets(char * restrict s, int n, FILE * restrict stream){
int ch, i=0;
if(n<1)return NULL;
if(n==1){
*s = '\0';
return s;
}
while(EOF!=(ch=fgetc(stream))){
s[i++] = ch;
if(i == n - 1) break;
if(ch == '\n'){
char next = fgetc(stream);
if(next == '\n')
break;
else
ungetc(next, stream);
}
}
s[i] = '\0';
return i == 0 ? NULL : s;
}
int main(int argc, char *argv[]){
char str[100];
printf("input (only newline is end)\n");
mfgets(str, 100, stdin);
printf("%s", str);
return 0;
}

Resources