Substring in c programming: c programming code to find a substring from a given string and for all substrings of a string, For example substrings of string “the” are “t”, “th”, “the”, “h”, “he” and “e” to find substring we create our own c substring function which returns a pointer to string. String address, length of substring required and position from where to extract substring are the three arguments passed to function. String.h does not contain any library function to directly find substring.

C substring code

[ad type=”banner”] [pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0Aint%20main()%20%0A%7B%0A%20%20%20char%20string%5B1000%5D%2C%20sub%5B1000%5D%3B%0A%20%20%20int%20position%2C%20length%2C%20c%20%3D%200%3B%0A%20%0A%20%20%20printf(%22Input%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20printf(%22Enter%20the%20position%20and%20length%20of%20substring%5Cn%22)%3B%0A%20%20%20scanf(%22%25d%25d%22%2C%20%26position%2C%20%26length)%3B%0A%20%0A%20%20%20while%20(c%20%3C%20length)%20%7B%0A%20%20%20%20%20%20sub%5Bc%5D%20%3D%20string%5Bposition%2Bc-1%5D%3B%0A%20%20%20%20%20%20c%2B%2B%3B%0A%20%20%20%7D%0A%20%20%20sub%5Bc%5D%20%3D%20’%5C0’%3B%0A%20%0A%20%20%20printf(%22Required%20substring%20is%20%5C%22%25s%5C%22%5Cn%22%2C%20sub)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Substring in c programming

We create a function and pass it four arguments original string array, substring array, position and length of desired substring. As call by reference is used we do not need to return substring array. See another code below in which we return pointer to substring, which we create in our array using dynamic memory allocation.

[ad type=”banner”] [pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%20%0Avoid%20substring(char%20%5B%5D%2C%20char%5B%5D%2C%20int%2C%20int)%3B%0A%20%0Aint%20main()%20%0A%7B%0A%20%20%20char%20string%5B1000%5D%2C%20sub%5B1000%5D%3B%0A%20%20%20int%20position%2C%20length%2C%20c%20%3D%200%3B%0A%20%0A%20%20%20printf(%22Input%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20printf(%22Enter%20the%20position%20and%20length%20of%20substring%5Cn%22)%3B%0A%20%20%20scanf(%22%25d%25d%22%2C%20%26position%2C%20%26length)%3B%0A%20%0A%20%20%20substring(string%2C%20sub%2C%20position%2C%20length)%3B%0A%20%0A%20%20%20printf(%22Required%20substring%20is%20%5C%22%25s%5C%22%5Cn%22%2C%20sub)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20%0Avoid%20substring(char%20s%5B%5D%2C%20char%20sub%5B%5D%2C%20int%20p%2C%20int%20l)%20%7B%0A%20%20%20int%20c%20%3D%200%3B%0A%20%0A%20%20%20while%20(c%20%3C%20l)%20%7B%0A%20%20%20%20%20%20sub%5Bc%5D%20%3D%20s%5Bp%2Bc-1%5D%3B%0A%20%20%20%20%20%20c%2B%2B%3B%0A%20%20%20%7D%0A%20%20%20sub%5Bc%5D%20%3D%20’%5C0’%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

C substring program using pointers

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0Achar*%20substring(char*%2C%20int%2C%20int)%3B%0A%20%0Aint%20main()%20%0A%7B%0A%20%20%20char%20string%5B100%5D%2C%20*pointer%3B%0A%20%20%20int%20position%2C%20length%3B%0A%20%0A%20%20%20printf(%22Input%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20printf(%22Enter%20the%20position%20and%20length%20of%20substring%5Cn%22)%3B%0A%20%20%20scanf(%22%25d%25d%22%2C%26position%2C%20%26length)%3B%0A%20%0A%20%20%20pointer%20%3D%20substring(%20string%2C%20position%2C%20length)%3B%0A%20%0A%20%20%20printf(%22Required%20substring%20is%20%5C%22%25s%5C%22%5Cn%22%2C%20pointer)%3B%0A%20%0A%20%20%20free(pointer)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20%0A%2F*C%20substring%20function%3A%20It%20returns%20a%20pointer%20to%20the%20substring%20*%2F%0A%20%0Achar%20*substring(char%20*string%2C%20int%20position%2C%20int%20length)%20%0A%7B%0A%20%20%20char%20*pointer%3B%0A%20%20%20int%20c%3B%0A%20%0A%20%20%20pointer%20%3D%20malloc(length%2B1)%3B%0A%20%0A%20%20%20if%20(pointer%20%3D%3D%20NULL)%0A%20%20%20%7B%0A%20%20%20%20%20%20printf(%22Unable%20to%20allocate%20memory.%5Cn%22)%3B%0A%20%20%20%20%20%20exit(1)%3B%0A%20%20%20%7D%0A%20%0A%20%20%20for%20(c%20%3D%200%20%3B%20c%20%3C%20length%20%3B%20c%2B%2B)%0A%20%20%20%7B%0A%20%20%20%20%20%20*(pointer%2Bc)%20%3D%20*(string%2Bposition-1)%3B%20%20%20%20%20%20%0A%20%20%20%20%20%20string%2B%2B%3B%20%20%20%0A%20%20%20%7D%0A%20%0A%20%20%20*(pointer%2Bc)%20%3D%20’%5C0’%3B%0A%20%0A%20%20%20return%20pointer%3B%0A%7D” message=”” highlight=”” provider=”manual”/] [ad type=”banner”]

C code for all substrings of a string

[pastacode lang=”c” manual=”%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%23include%20%3Cmalloc.h%3E%0A%20%0Achar*%20substring(char*%2C%20int%2C%20int)%3B%0A%20%0Aint%20main()%20%0A%7B%0A%20%20%20char%20string%5B100%5D%2C%20*pointer%3B%0A%20%20%20int%20position%20%3D%201%2C%20length%20%3D%201%2C%20temp%2C%20string_length%3B%0A%20%0A%20%20%20printf(%22Enter%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20temp%20%3D%20string_length%20%3D%20strlen(string)%3B%0A%20%0A%20%20%20printf(%22Substring%20of%20%5C%22%25s%5C%22%20are%5Cn%22%2C%20string)%3B%0A%20%0A%20%20%20while%20(position%20%3C%3D%20string_length)%0A%20%20%20%7B%0A%20%20%20%20%20%20while%20(length%20%3C%3D%20temp)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20pointer%20%3D%20substring(string%2C%20position%2C%20length)%3B%0A%20%20%20%20%20%20%20%20%20printf(%22%25s%5Cn%22%2C%20pointer)%3B%0A%20%20%20%20%20%20%20%20%20free(pointer)%3B%0A%20%20%20%20%20%20%20%20%20length%2B%2B%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20temp–%3B%0A%20%20%20%20%20%20position%2B%2B%3B%0A%20%20%20%20%20%20length%20%3D%201%3B%0A%20%20%20%7D%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20″ message=”” highlight=”” provider=”manual”/]

Output:

Substring in c programming

Categorized in: