0

I need to create a program in which I could how many times a word appears in a string, and I included * because of strchr and my program works if I put this for example

int main(void) {
  const char *needle, *stack;

  needle = "a"; stack = "aaa";
  printf("[%s] exists %d times in [%s]\n", needle, count(needle, stack), stack);
return 0;
}

and output would be 3, you have "a" appearing 3 times in "aaa" , but if for example I try to get stack from std input with

scanf("[%s]", &stack);

my program doesn't work, also I would like to create a 2D version of this so I need to merge chars from a 2D array into a string also, but if I put them into an array of chars, my program doesn't work also. Please don't suggest me other soutions to solve this problem because it has to be done similar to this.

3 Answers3

1

There is one problems with your scanf call, and when that is fixed you have two others.

The first problem is that you pass a pointer to the pointer (i.e. const char **) as argument to scanf. While the arguments should be pointers, stack already is a pointer so you don't have to use the address-of operator to get a pointer.

The second problem when you fix the first one is char stack is a pointer to constant data, and writing to constant data leads to undefined behavior. The third problem is that stack is a pointer, but you don't allocate (at least not in the program you show us) memory that it can point to.

The two later problems can easily be solved by using a non-constant array instead:

char stack[32];
scanf("%31s", stack);

I've changed the format so that scanf won't read more than 31 characters and put into your array. It's 31 because all strings in C needs a special terminator character as well.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
1

When declaring stack as a const char* there are 2 problems :

  • You can't declare it const because you will modify the values in the scanf

  • You don't allocate space for stack to allow the storage of the data read

You don't have the problem with needle, because when doing needle = "a";, the needle pointer will point to a which is already statically allocated in the .data section and is const char*

To fix this, you have 2 solutions :

int main(void) {
   const char *needle;
   char stack[200];
   needle = "a";
   scanf("%199s", &stack);
   printf("[%s] exists %d times in [%s]\n", needle, count(needle, stack), stack);
   return 0;
}

Or by dynamically allocating memory on the heap:

int main(void) {
   const char *needle;
   char *stack = new char[200];
   needle = "a";
   scanf("%199s", stack);
   printf("[%s] exists %d times in [%s]\n", needle, count(needle, stack), stack);

   delete[] stack;
   return 0;
}
naab
  • 997
  • 1
  • 7
  • 23
0

to read the string from use use,

scanf("%s",stack);

this is the format to read a string in C using scanf(). you can read this related question.
You also need to allocate memory to variable stack.

Community
  • 1
  • 1
LearningC
  • 3,128
  • 1
  • 8
  • 19