-1

I am fairly new to C programming and trying to improve. I have seen a few question similar to this and tried to implement their suggestions using strncpy but it still won't work. Can anyone give me a pointer in the right direction? Thanks.

The aim of the code is to be given a string and split it on finding a blank space. I am deliberately not using strtok().

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char str[10] ;
char word[10] ;
int i, j, k, l ;

  int main()
{
  printf("Please enter a string of ten characters and I'll find the blanks\n") ;
  fgets(str,10,stdin) ;

  printf("Original string is %s\n", str) ;

  j = 0 ;

  for(i == 0 ; i < 10 ; i++){
    if(!isblank(str[i])){
      strncpy(word[j], &str[i], 1) ;
      printf("i = %d, j = %d, str[i] = %c, word[j] = %c\n",i, j, str[i], word[j]) ;
      j++ ;
    }else{
      printf("%s\n",word) ;
      j = 0 ;
    }
  }
  return 0 ;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
James
  • 223
  • 1
  • 3
  • 9
  • This is a real issue with strtok() and strtok_r() on non-BSD system where strsep is not available. Therefore i am up voting the post that has been down-voted. – Beezer Dec 06 '20 at 15:24

3 Answers3

5

The problem, as I see it, is in

for(i == 0 ; i < 10 ; i++)
      ^^

You should be using the assignment (=), like

for(i = 0 ; i < 10 ; i++)

to set the initial value of i. Otherwise, by accessing the value of an uninitialized local variable (whose value is indeterminate without an initialization), your code will invoke undefined behavior.

Then, the usage of strncpy() also looks wrong, you're not passing a char *, as required for the first argument.

Now, that said, two more things,

  1. To copy a single char one at a time, you can simply use the assignment , like

    word[j] = str[i];
    
  2. You should only loop over the input array till the valid entries, not the whole size (10). In case, the input is smaller, you'll again hit UB.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

I think your problem is no pointer to the dest argument in the strncpy function.

strncpy(word[j], &str[i], 1) ;
        ^

Add a pointer to the destination for the strncpy function

strncpy(&word[j], &str[i], 1) ;

as well as fixing the issue mentioned by Sourav Ghosh and you should have it.

D.Bag
  • 31
  • 3
0

As i commented on the @James, this is a REAL issue on non-BSD where there is no strsep(). strtok() and strtok_r() are both IMO fundamentally flawed in that they unilaterally skip any sequence of >1 adjacent delimiters.

I used this solution: https://www.geeksforgeeks.org/c-program-replace-word-text-another-given-word/ in order to fill the above with space gaps so that I can get a proper tokenization of ALL my payload. Reader beware, the solution above has a flaw...so you might need to call it more than once...Not optimal, but if you are in a hurry as I was, I used it and it works.

  while(strstr(token, ";;")!=NULL) {
                 token = replaceWord(token, ";;", "; ;");
                 (void) sprintf (v_sg_TempMsg, "Massaged the input agsId to token:[%s]", token);
                 printf(v_sg_TempMsg);
                 ++tokenCounter;
                 if(tokenCounter==15) {
                     break;
                 }
   }//while(strstr(token, ";;")!=NULL) {

...and the above is a hack to get call the code that is flawed to eventually remove all the occurrences of the delimiters...this is dirty dirty code, so please, no flamers...it is here to HELP people who are blocked because of a lack of an alternative to strtok() and strtok_r() skipping.

Beezer
  • 1,084
  • 13
  • 18