Thread: Escape sequence search

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    38

    Question Escape sequence search

    Hey all,

    I believe I have a very basic question for you all.. But I have tried and tried and cant get it to work to save my life.. I am wanting to return the index of the first '\n' that appears in a string. The escape sequence search is carried out in the sub function below.. The entire function (to read file line by line) is below also..

    I am trying to get the string "aaa\nbbb\nccc" to return 4 (the position of the first '\n'....... Any idea why it isnt stopping?!

    The sub function is :
    Code:
    static int        check_line(char *str)
    {
            int i;
    
            i = 0;
            while (str)
            {
            printf("i is %d\n", i);
                    if (str[i] == '\n')
                            return (i);
                    i++;
            }
            return (0);
    }
    My entire function code is :
    Code:
    #include "line_by_line.h"
    #include <stdio.h>
    
    static int        check_line(char *str)
    {
            int i;
    
            i = 0;
            while (str)
            {
            printf("i is %d\n", i);
                    if (str[i] == '\n')
                            return (i);
                    i++;
            }
            return (0);
    }
    
    static char        *ft_trim_line(char *str)
    {
            char    *ret;
            int        i;
    
            i = 0;
            while (str[i] != '\n')
                    i++;
            ret = ft_strnew(i);
            i = 0;
            while (str[i] != '\n')
            {
                    ret[i] = str[i];
                    i++;
            }
            ret[i] = '\0';
            return (ret);
    }
    
    static char        *ft_get_remainder(char *str)
    {
            int        i;
            char    *ret;
            i = 0;
    
            while (str[i] != '\n')
                    i++;
            ret = ft_strsub(str, i + 1, ft_strlen(str) - 1); 
            return (ret);
    }
    
    int            gl(const int fd, char **line)
    {
            char buf[BUFF_SIZE + 1];
            static char *str;
            char *tmp;
            ssize_t ret;
            int index;
    
            if (BUFF_SIZE < 1 || !line || (fd < 0))
                    return (-1);
            index = 0;
            if (!str)
                    str = ft_memalloc(1);
            while ((ret = read(fd, buf, BUFF_SIZE)) > 0)
            {
                    if (ret == -1)
                            return (-1);
                    buf[BUFF_SIZE] = '\0';
                    str = ft_strjoin(str, buf);
                    if ((index = check_line(str)) != BUFF_SIZE)
                    {
                            *line = ft_trim_line(str);
                            tmp = ft_strdup(str);
                            ft_strclr(str);
                            str = ft_strnew(BUFF_SIZE - index);
                            str = ft_get_remainder(tmp);
                            return(1);
                    }
            }
            return (0);
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,729
    check_line() terminates just fine, it gives the correct answer too (3 for your string, not 4). The problem is elsewhere.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    A few thoughts:
    * in your example "aaa\nbbb\nccc" the position of the \n is 4 so the correct index is 3 - remember it's zero-based.

    * why return 0 when there is no \n , 0 is a valid index

    An easier way
    Code:
    /* returns the index of ch in str or -1 if not found 
        usage: int res = indexof("Hello world", ' '); if (res == -1) handle not found
    */
    int indexof(const char *str, char ch)
    {
      int i;
      for (i = 0; str[i] != '\0'; i++)
        if (str[i] == ch)
          return i;
    
      return -1;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Escape sequence
    By Charles Ontiti in forum C Programming
    Replies: 4
    Last Post: 02-26-2015, 11:00 AM
  2. What is the use of the '/?' escape sequence?
    By figarojones in forum C Programming
    Replies: 7
    Last Post: 11-13-2012, 02:04 PM
  3. escape sequence help please
    By Shinzu911 in forum C Programming
    Replies: 1
    Last Post: 05-03-2012, 04:34 PM
  4. Escape sequence for &
    By 3saul in forum C Programming
    Replies: 10
    Last Post: 02-28-2006, 10:01 PM
  5. escape sequence
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 08-01-2002, 08:36 AM

Tags for this Thread