Can anyone tell me how to search in a string?

Recommended Answers

All 5 Replies

look for a list of string functions in C.

Ok, here's what I want to do.
I want this:

1 byte DEC A
2 bytes MOV R5,#immed
3 bytes CJNE R4,#immed,offset

to be like this:

DEC A
MOV R5,
#immed
CJNE R4,
#immed,
offset

I want to split the lines depending on the number of bytes each string has, then eliminating the number of bytes from the output

Here's my code so far:

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

int main ()
{
  FILE *input, *output;
  input = fopen("input4.txt","r");
  output = fopen("output5.txt","w");
  char a[10000];
  char *str;
  int x;
  while(!feof(input))
  {
    fscanf(input, "%s", &a[x]); 
    if(a[0]=='1')
    {
      continue;
    }
    if(a[0]=='2')
    {
      str = strtok (a,",");
    }
    if(a[0]=='3')
    {
      str = strtok (a," ,");
    }
    
    while (str != NULL)
    {
      if(a[0]=='1')
      {
        fprintf (output, "%s\n",str);
      }
      if(a[0]=='2')
      {
        fprintf (output, "%s\n",str);
        str = strtok (NULL, ",");
      }
      if(a[0]=='3')
      {
        fprintf (output, "%s\n",str);
        str = strtok (NULL, " ,");
      }
    }
  }
  fclose(input);
  fclose(output); 
  return 0;
}

Can anyone give me tips on what to do next?
My problem here is that the output only shows the numbers 2 and 3 and disregards 1

The output is:
2
3

Well as far as I can tell you are using the variable x but never initializing it and never changing it so it's functioning more like a constant with unknown value. You are reading data into a[x], but making "if" statements having to do with a[0]. Perhaps x is equal to 0, perhaps not, but if you want it to be 0, initialize it to 0 to be sure.

Try something like that.

main()
{
    char no[5], str[10], cmd[5], op[20];
    FILE* input = fopen("input4.txt","r");
    while(!feof(input))
    {
        fscanf(input, "%s %s %s %s", &no, &str, &cmd, &op); //no, str, cmd, operand
        
        char *search=",";
        char *op1=strtok(op, search);
        char *op2=strtok(NULL,search);
        char *op3=strtok(NULL,search);
        
        //you can transform this into some other output while avoiding NULLs with if etc.       
        printf("%s %s %s %s %s %s\n", no, str, cmd, op, op2, op3);
    }
}

Thanks for the help. I finally got the result I wanted

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.