jwkoo
jwkoo

Reputation: 2653

string length in c without using string library

    char* str =
    "\
    a-a-a-a\
    differing the text, because that was the lecture thing\
    the text has been changed\
    I know!\
    the text has been changed\
    ";

i deeply thinking about this for hours but can`t figure it out.. with using only stdio.h

string.h is not allowed, but using only basic things..

how can I get string length? someone please help me.

the goal is to find frequency of input pattern in a given string

ex) ha => 2, di => 1..

help me.

Upvotes: 2

Views: 2960

Answers (3)

EsmaeelE
EsmaeelE

Reputation: 2658

This works for some test input string, but i higly recommend to check it with more cases.

Suppose we have implemented strstr().

strstr()

Is a C library function from string.h Library

char *strstr(const char *haystack, const char *needle)

This function finds the first occurrence of the substring needle in the source string haystack.

The terminating \0 characters are not compared.

source: TutorialsPoint (with some edition)

Code

#include <stdio.h>

#include <string.h>


unsigned int Neostrlen(const char* str)
{
    unsigned int length = 0;
    while (*str != 0)
    {
        str++;
        length++;
    }
    return length;
}

int BluePIXYstrlen(char* str)
{
    int len = 0;
    sscanf(str, "%*[^0]%n", &len);

    return len;
}

int Jeanfransvastrlen(char* str)
{
    int i;
    for (i=0;str[i];i++);   
    return i;
}    


int main(int argc, char **argv){

//is it true, no need to malloc????

char* str =
    "\
    P-P-A-P\
    I have a pen, I have a apple\
    Uh! Apple-Pen!\
    I have a pen, I have pineapple\
    Uh! Pineapple-Pen!\
    Apple-Pen, Pineapple-Pen\
    Uh! Pen-Pineapple-Apple-Pen\
    Pen-Pineapple-Apple-Pen\
    ";


    printf("len: %d\n", Jeanfransvastrlen(str));
    printf("len: %d\n", Neostrlen(str));
    printf("len: %d\n", BluePIXYstrlen(str));

    printf("sss:%s\n\n\n", str);

    char * search = "have";//search for this substring
    int lenSr= Neostrlen(search);

    printf("lenSr: %d\n", lenSr); 


    char * ret;
    ret = strstr(str, search);

    int count = 0;

    while (ret){
        //printf("The substring is: %s\n\n\n\n", ret);
        count++;
        for (int i=0;i<lenSr;i++){
            printf("%c", ret[i]);
        }

            printf("\nEnd sub\n");


        for (int i=0;i<lenSr;i++){
            ret++;      
        }
        ret = strstr(ret, search);
    }
    printf("count: %d\n", count);


    return 0;
}

Edited

For only using stdio.h you can substitute all strstr() with this version of mystrstr() adopted from leetcode

mystrstr()

char* mystrstr(char *str, const char *target) {

  if (!*target) {
    return str;
  }

  char *p1 = (char*)str;

  while (*p1) {

    char *p1Begin = p1, *p2 = (char*)target;
    while (*p1 && *p2 && *p1 == *p2) {
      p1++;
      p2++;

    }

    if (!*p2){
      return p1Begin;
    }

    p1 = p1Begin + 1;
  }

  return NULL;
}

Hint

  1. I removed const from first first argument of mystrstr() because of I want to change it later, and this is the only changed i have made on original code.

  2. This version is sensitive to Uppercase and lowercase letters in string, for example Apple is differ from apple.

  3. As chux said in comments my code return substrings of "ababa" from source "aba" only {aba} not more. and this is because i change string pointer inside while in last for.

Suggestion

Try to implement your version of strstr(), and strlen()

Upvotes: 0

Neo
Neo

Reputation: 3786

As for length of string, the implementation of strlen isn't very complicated.
All you should do is to loop over the string until you find a \0 (end of string) and count the number of times you looped.

unsigned int mystrlen(const char* str)
{
    unsigned int length = 0;
    while (*str != 0)
    {
        str++;
        length++;
    }
    return length;
}

This could be shortened into

unsigned int len = 0;
for (; str[len]; len++);

Upvotes: 5

chris01
chris01

Reputation: 12321

A string in pure C is just a pointer to a memory. IF the last element is 0, then you can use strlen or whatever checks for that. But if that is not the case you need to memorize the length in a variable.

So if it is 0-terminated just loop to the first element that is 0 (not '0') and thats the end. If you counted the elements you have the string-length.

Upvotes: 0

Related Questions