开发者

Basic string.h question (C++)

开发者 https://www.devze.com 2023-03-04 13:45 出处:网络
I have a simple C++ file, and it strims characters from a text file, although I would like to change it.

I have a simple C++ file, and it strims characters from a text file, although I would like to change it.

The text strings are as follows:

XXXXX|YYYYYYYYYYYYYYYYYYY

At current it removes

|YYYYYYYYYYYYYYYYYYYY

from the string, although I would like it to remove:

XXXXX|

instead, so in effect take out the left side rather than the right-hand side.

The code I have at the moment is:

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

main(int argc, char *argv[])
{
    char s[2048], *pos=0;
    while (fgets(s, 2048, stdin))
    {
        if (pos开发者_开发知识库 = strpbrk(s, "|\r\n"))
            *pos='\0';
        puts(s);
    }
    return 0;
}


You should seldom be using <string.h> in a C++ program.

You should probably be using <string>; you might perhaps use <cstring>.

That's without even looking at your code - just forget that <string.h> exists if you are writing C++; it is a C header for C functionality. Similar comments apply to <stdio.h>; it is a C header and should seldom be used in C++ (using <iostream> normally, or occasionally <cstdio>).

Your main() function needs a return type of int (in both C++ and C99). Since you want the information after the pipe, you could write (a perfectly valid C (C89, C99) program - not using any distinctive feature of C++ at all, though a C++ compiler will also accept it):

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

int main(int argc, char *argv[])
{
    char s[2048];
    while (fgets(s, sizeof(s), stdin))
    {
        char *pos = strpbrk(s, "|\r\n");
        if (pos != 0)
            fputs(pos+1, stdout);
    }
    return 0;
}

Using fputs() instead of puts() to avoid double-spacing the output.


Use another array and while you read the XXXXX assign the characters in the target array. When you encounter the | in the sour source string, assign a '\0' in the destination string. Or if you do not want to use another auxiliary array then you need to shift the starting of the part with 'Y's to the front of the array. To do this you fix one counter to the base of the array (i) (where you want the next part to be shifted), and then scan the string upto the part with the first 'Y' store this to another counter (j). Now while j

This is the shift version, using only one array.

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

int main (void)
{
  char s[1024];
  int i, j, n;
  printf ("\ns: ");
  scanf (" %[^\n]", s);
  n = strlen (s);

  i = j = 0;
  /* Find the dilimeter index */
  while ((j < n) && s[j] != '|')
   j++;
  /* Move the other side of the dilimeter to
   * the begining pointed by index i. note that
   * we avoid the delimeter to be copied by 
   * pre-incrementing the counter j
   */
  while (j<n)
   s[i++] = s[++j];

  s[i] = '\0'; /* Terminate String */

  printf ("\n%s\n", s);
  return 0;
}

The 2 array version is in this case essentially the same.


strpbrk() returns the location of the '|', so just use that to output the left-trimmed string. The following will output |YYYYYYYYYYYYYYYY. You'll need to modify it slightly to remove the leading '|'.

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

main(int argc, char *argv[])
{
    char s[2048], *pos=0;
    while (fgets(s, 2048, stdin))
    {
        if (pos = strpbrk(s, "|\r\n"))
            puts(pos);
    }
    return 0;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号