Generate all ip addresses from given string - c

Write a program that determines where to add periods to a decimal string so that the resulting string is a
valid IP address. There may be more than one valid IP address corresponding to a string, in which case you
should print all possibilities. Additionally your program must validate that neither group must not exceed
255.
For example:
If the mangled string is “19216811” then two corresponding IP addresses are 192.168.1.1 and 192.216.81.1.
There are also seven other possible IP addresses for this string.
My program is causin segmentationn fault
My Code:
#include<stdio.h>
#include <string.h>
#include<stdbool.h>
#include<stdlib.h>
bool is_valid_part(char *part) {
return (strlen(part)== 1) || (part[0] != 0 && atoi(part) <= 255);
}
int a =0;
int pos = 0;
void generate(char *ip, char **ans)
{
int l = strlen(ip);
// Check for string size
if (l > 12 || l < 4) {
printf("Not valid IP String");
return;
}
char *check = ip;
// Generating different combinations.
for (int i = 1; i < l - 2; i++) {
for (int j = i + 1; j < l - 1; j++) {
for (int k = j + 1; k < l; k++) {
check = strncpy(check, ip, k); + '.'+ strncpy(check, ip + k, strlen(ip) - k - 1);
check = strncpy(check, ip, j); + '.'+ strncpy(check, ip + j, strlen(ip) - j - 1);
check= strncpy(check, ip, i); + '.'+ strncpy(check, ip + i, strlen(ip) - i - 1);
if (is_valid_part(check)) {
ans[pos++] = check;
printf("%s", check);
}
check = ip;
}
}
}
}
int main(void){
char str[32];
fgets(str, 32, stdin);
printf("%s",str);
char **ans;
generate(str, ans);
}

As the input string str contains a trailing newline character,
strlen(str) is larger than the intended value. You need to chop
the newline character off.
Your is_valid_part() function is very close but you need to
change part[0] != 0 to part[0] != '0' to examine the leading 0.
If you want to create a list of ip addresses at runtime, you need to
allocate memory area with malloc/realloc and free the area after use.
Then would you please try the following:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
bool is_valid_part(char *part) {
return (strlen(part) == 1) || (part[0] != '0' && atoi(part) <= 255);
}
void generate(char *ip, char ***ptr, int *pos)
{
int len = strlen(ip);
char octet[4][4];
char addr[16];
// check for string size
if (len > 12 || len < 4) {
printf("The length must be between 4 and 12\n");
return;
}
// check characters
for (int i = 0; i < len; i++) {
if (! isdigit(ip[i])) {
printf("Invalid character: %c\n", ip[i]);
return;
}
}
// generating different combinations.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
for (int k = 1; k <= 3; k++) {
int l = len - i - j - k;
if (l < 1 || l > 3) continue;
strncpy(octet[0], ip, i); *(octet[0] + i) = '\0';
strncpy(octet[1], ip + i, j); *(octet[1] + j) = '\0';
strncpy(octet[2], ip + i + j, k); *(octet[2] + k) = '\0';
strncpy(octet[3], ip + i + j + k, l); *(octet[3] + l) = '\0';
int fail = 0;
for (int m = 0; m < 4; m++) {
if (! is_valid_part(octet[m])) fail = 1;
}
if (fail) continue; // skip to next combination
// generate a string of ip address
strcpy(addr, octet[0]);
for (int m = 1; m < 4; m++) {
strcat(addr, ".");
strcat(addr, octet[m]);
}
// printf("%s\n", addr);
// allocate memory and append the string to the list
*ptr = realloc(*ptr, (*pos + 1) * sizeof(char **));
(*ptr)[*pos] = malloc(strlen(addr) + 1);
strcpy((*ptr)[*pos], addr);
(*pos)++; // increment the count of ip addresses
}
}
}
}
int main(void) {
char str[BUFSIZ];
printf("Enter a decimal string: ");
fgets(str, BUFSIZ, stdin);
char *p = rindex(str, '\n'); // trim the newline character
if (p) *p = '\0';
p = rindex(str, '\r'); // trim the carrage return character
if (p) *p = '\0';
// printf("%s\n", str);
char **ans = NULL; // array of ip address strings
int cnt = 0; // counter of the ip addresses
generate(str, &ans, &cnt);
// print the results
for (int i = 0; i < cnt; i++) {
printf("%s\n", ans[i]);
}
// free memory
for (int i = 0; i < cnt; i++) {
free(ans[i]);
}
free(ans);
}
Output:
Enter a decimal string: 19216811
1.92.168.11
19.2.168.11
19.21.68.11
19.216.8.11
19.216.81.1
192.1.68.11
192.16.8.11
192.16.81.1
192.168.1.1
Integer values i, j, k and l are generated such that each value
is between 1 and 3 inclusive and the sum equals to strlen(ip).
Then the string ip is divided into an array octet, each of
length i, j, k and l.
Each substring in octet is terminated with a null character.
If every elements of octet meet the condition, they are concatenated
into an ip address string.
*ptr is a 2-d array which grows on the fly.
As the value of *ptr is modified within the generate() function,
a pointer to ans is passed to the function.
The counter cnt is also passed by address. It is not recommended to
use a global address as long as there are alternatives.

Related

How to modify a char pointer passed as parameter in a function?

So, I am doing my own rudimentary version of itoa(), and I just realized I don't exactly know how to modify a char* passed as parameter, or if there is something wrong with the way I am doing it...
The way I am doing it is by doing malloc() onto the buffer passed as argument, then write the number into it. This is apparently working before returning from the function (the buffer is printed correctly), but then when trying to print it back in main(), it segfaults.
If I understand the error correctly, I am changing the address buff points to inside with that malloc(), and then modify its contents, but the new malloc'd address inside is not returned. How could I do that without changing the parameters or the return value?
int itoa(int i, char *buff) {
int length = 0;
// get the length
long temp = 1;
while (temp <= i) {
length++;
temp *= 10;
}
buff = malloc(length + 1); // buff will have 'length' chars + one extra (\0)
int j = 0;
do { /* generate digits in reverse order */
buff[j++] = i % 10 + '0'; /* get next digit */
} while ((i /= 10) > 0); /* delete it */
buff[length] = '\0';
// reverse it
int k, l;
char c;
for (k = 0, l = length - 1; k<l; k++, l--) {
c = buff[k];
buff[k] = buff[l];
buff[l] = c;
}
printf("buff's now:%s\n", buff);
return 0;
}
int main() {
char *buff = NULL;
itoa(45, buff);
printf("%s\n", buff);
}
Your pointer isn't modified as it was copied. You can read more here. You can try this code after reading the above link.
#include <stdlib.h>
#include <stdio.h>
int itoa_(int i, char **parabuff)
{
int length = 0;
// get the length
long temp = 1;
while (temp <= i)
{
length++;
temp *= 10;
}
char *buff = malloc(length + 1); // buff will have 'length' chars + one extra (\0)
int j = 0;
do
{ /* generate digits in reverse order */
buff[j++] = i % 10 + '0'; /* get next digit */
} while ((i /= 10) > 0); /* delete it */
buff[length] = '\0';
// reverse it
int k, l;
char c;
for (k = 0, l = length - 1; k < l; k++, l--)
{
c = buff[k];
buff[k] = buff[l];
buff[l] = c;
}
printf("buff's now: %s\n", buff);
*parabuff = buff;
return 0;
}
int main()
{
char *buff = NULL;
itoa_(45, &buff);
printf("buff in main: %s\n", buff);
}
//OUTPUT
buff's now: 45
buff in main: 45

Edit Distance Matrix

I'm trying to build a program that takes two strings and fills in the edit distance matrix for them. The thing that is tripping me up is, for the second string input, it is skipping over the second input. I've tried clearing the buffer with getch(), but it didn't work. I've also tried switching over to scanf(), but that resulted in some crashes as well. Help please!
Code:
#include <stdio.h>
#include <stdlib.h>
int min(int a, int b, int c){
if(a > b && a > c)
return a;
else if(b > a && b > c)
return b;
else
return c;
}
int main(){
// allocate size for strings
int i, j;
char *input1 = (char*)malloc(sizeof(char)*100);
char *input2 = (char*)malloc(sizeof(char)*100);
// ask for input
printf("Enter the first string: ");
fgets(input1, sizeof(input1), stdin);
printf("\nEnter the second string: ");
fgets(input2, sizeof(input2), stdin);
// make matrix
int len1 = sizeof(input1), len2 = sizeof(input2);
int c[len1 + 1][len2 + 1];
// set up input 2 length
for(i = 0; i < len2 + 1; i++){
c[0][i] = i;
}
// set up input 1 length
for(i = 0; i < len1 + 1; i++){
c[i][0] = i;
}
// fill in the rest of the matrix
for(i = 1; i < len1; i++){
for(j = 1; j < len2; j++){
if(input1[i] == input2[j]) // if the first letters are equal make the diagonal equal to the last
c[i][j] = c[i - 1][j - 1];
else
c[i][j] = 1 + min(c[i - 1][j - 1], c[i - 1][j], c[i][j - 1]);
}
}
// print the matrix
printf("\n");
for(j = 0; j < len2; j++){
for(i = 0; i < len1; i++){
printf("| %d", c[i][j]);
}
printf("\n");
}
return 1;
}
Stick with fgets.
As others have pointed out, use char input1[100] instead of char *input1 = malloc(...)
But, even with that change, which makes the sizeof inside of the fgets correct, using sizeof when setting up len1 and len2 is wrong. You'll be processing an entire buffer of 100, even if their are only 10 valid characters in it (i.e. the remaining ones are undefined/random).
What you [probably] want is strlen [and a newline strip] to get the actual useful lengths.
Here's the modified code [please pardon the gratuitous style cleanup]:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
min(int a, int b, int c)
{
if (a > b && a > c)
return a;
if (b > a && b > c)
return b;
return c;
}
int
main(void)
{
// allocate size for strings
int i;
int j;
char input1[100];
char input2[100];
// ask for input
printf("Enter the first string: ");
fgets(input1, sizeof(input1), stdin);
int len1 = strlen(input1);
if (input1[len1 - 1] == '\n') {
input1[len1 - 1] = 0;
--len1;
}
printf("\nEnter the second string: ");
fgets(input2, sizeof(input2), stdin);
int len2 = strlen(input2);
if (input2[len2 - 1] == '\n') {
input2[len2 - 1] = 0;
--len2;
}
// make matrix
int c[len1 + 1][len2 + 1];
// set up input 2 length
for (i = 0; i < len2 + 1; i++) {
c[0][i] = i;
}
// set up input 1 length
for (i = 0; i < len1 + 1; i++) {
c[i][0] = i;
}
// fill in the rest of the matrix
for (i = 1; i < len1; i++) {
for (j = 1; j < len2; j++) {
// if the 1st letters are equal make the diagonal equal to the last
if (input1[i] == input2[j])
c[i][j] = c[i - 1][j - 1];
else
c[i][j] = 1 + min(c[i - 1][j - 1], c[i - 1][j], c[i][j - 1]);
}
}
// print the matrix
printf("\n");
for (j = 0; j < len2; j++) {
for (i = 0; i < len1; i++) {
printf("| %d", c[i][j]);
}
printf("\n");
}
return 1;
}
UPDATE:
Okay sweet I see what you mean! The reason I was trying to use malloc though was to avoid making the matrix that I had to print a size of 100x100 blank spaces.
With either the fixed size input1 or the malloced one, fgets will only fill it to the input size entered [clipped to the second argument, if necessary]. But, it does not pad/fill the remainder of the buffer with anything (e.g. spaces on the right). What it does do is add an EOS [end-of-string] character [which is a binary 0x00] after the last char read from input [which is usually the newline].
Thus, if the input string is: abcdef\n, the length [obtainable from strlen] is 7, input[7] will be 0x00, and input1[8] through input1[99] will have undefined/random/unpredictable values and not spaces.
Since a newline char isn't terribly useful, it is often stripped out before further processing. For example, it isn't terribly relevant when computing edit distance for a small phrase.
Does using strlen() only count the number of chars inside the array, or does it include all the blank spaces too?
As I mentioned above, fgets does not pad the string at the end, so, not to worry. It will do what you want/expect.
strlen only counts chars up to [but not including the EOS terminator character (i.e.) zero]. If some of these chars happen to be spaces, they will be counted by strlen--which is what we want.
Consider computing the edit distance between any two of the following phrases:
quick brown fox jumped over the lazy dogs
the quick brown fox jumped over lazy dogs
quick brown fox jumps over the lazy dog
In each case, we want strlen to include the [internal/embedded] spaces in the length calculation. That's because it is perfectly valid to compute the edit distance of phrases.
There is a valid usage for malloc: when the amount of data is too big to fit on the stack. Most systems have a default limit (e.g. under linux, it's 8 MB).
Suppose we were computing the edit distance for two book chapters [read from files], we'd have (e.g.):
char input1[50000];
char input2[50000];
The above would fit, but the c matrix would cause a stack overflow:
int c[50000][50000];
because the size of this would be 50000 * 50000 * 4 which is approx 9.3 GB.
So, to fit all this data, we'd need to allocate it on the heap. While it is possible to do a malloc for c and maintain the 2D matrix access, we'd have to create a function and pass off the pointer to c to it.
So, here's a modified version that takes input of arbitrarily large sizes:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define sysfault(_fmt...) \
do { \
fprintf(stderr,_fmt); \
exit(1); \
} while (0)
#define C(y,x) c[((y) * (len2 + 1)) + (x)]
long
min(long a, long b, long c)
{
if (a > b && a > c)
return a;
if (b > a && b > c)
return b;
return c;
}
char *
input(const char *prompt,long *lenp,const char *file)
{
FILE *fp;
char *lhs;
int chr;
long siz;
long len;
if (file != NULL)
fp = fopen(file,"r");
else {
fp = stdin;
printf("Enter %s string: ",prompt);
fflush(stdout);
}
lhs = NULL;
siz = 0;
len = 0;
while (1) {
chr = fgetc(fp);
if (chr == EOF)
break;
if ((chr == '\n') && (file == NULL))
break;
// grow the character array
if ((len + 1) >= siz) {
siz += 100;
lhs = realloc(lhs,siz);
if (lhs == NULL)
sysfault("input: realloc failure -- %s\n",strerror(errno));
}
lhs[len] = chr;
len += 1;
}
if (file != NULL)
fclose(fp);
if (lhs == NULL)
sysfault("input: premature EOF\n");
// add the EOS
lhs[len] = 0;
// return the length to the caller
*lenp = len;
return lhs;
}
int
main(int argc,char **argv)
{
long i;
long j;
char *input1;
long len1;
char *input2;
long len2;
long *c;
--argc;
++argv;
switch (argc) {
case 2:
input1 = input("first",&len1,argv[0]);
input2 = input("second",&len2,argv[1]);
break;
default:
input1 = input("first",&len1,NULL);
input2 = input("second",&len2,NULL);
break;
}
// make matrix
c = malloc(sizeof(*c) * (len1 + 1) * (len2 + 1));
if (c == NULL)
sysfault("main: malloc failure -- %s\n",strerror(errno));
// set up input 2 length
for (i = 0; i < len2 + 1; i++) {
C(0,i) = i;
}
// set up input 1 length
for (i = 0; i < len1 + 1; i++) {
C(i,0) = i;
}
// fill in the rest of the matrix
for (i = 1; i < len1; i++) {
for (j = 1; j < len2; j++) {
// if the 1st letters are equal make the diagonal equal to the last
if (input1[i] == input2[j])
C(i,j) = C(i - 1,j - 1);
else
C(i,j) = 1 + min(C(i - 1,j - 1), C(i - 1,j), C(i,j - 1));
}
}
// print the matrix
printf("\n");
for (j = 0; j < len2; j++) {
for (i = 0; i < len1; i++) {
printf("| %ld", C(i,j));
}
printf("\n");
}
return 1;
}

why the last number in doted decimal is not converted to binary?

#include<stdio.h>
#include<string.h>
#include <math.h>
long long convertDecimalToBinary(int n);
int main() {
int verify;
long long bip, dip;
char str1[100];
printf("Enter dotted decimal ip address :\n");
scanf("%s",str1);
verify = bin_verify(str1);
seperate(str1);
return 0;
}
int bin_verify(char str1[]) {
int i;
for(i = 0; i < strlen(str1); i++) {
if((str1[i] < 255) && (str1[i] > 0)) {
return 1;
}
}
}
// function to get first decimal no sepreted
int seperate(char str1[]) {
int s1, s2, s3, s4;
int i, j, k = 0, m, cnt = 0, cnt1 = 0, pos = 0;
char a[4], str2[100];
for(i = 0; i < strlen(str1); i++) {
pos = cnt;
if(str1[i] == '.') {
k = i;
pos = cnt;
for(j = 0; j < i; j++) {
a[j] = str1[k-cnt];
cnt = cnt - 1;
}
break;
}
else {
cnt++;
//goto one;
}
}
for(m = 0; m <= pos; m++) {
str1++;
}
s1 = atoi(a);
s1 = convertDecimalToBinary(s1);
printf("Binary Format of IP :\n");
printf("%d.",s1);
seperate2(str1);
return 0;
}
// function to get second decimal no sepreted
int seperate2(char str1[]) {
int s1, s2, s3, s4;
int i, j, k = 0, m, cnt = 0, cnt1 = 0, pos = 0;
char a[4], str2[100];
for(i = 0; i < strlen(str1); i++) {
pos = cnt;
if(str1[i] == '.') {
k = i;
pos = cnt;
for(j = 0; j < i; j++) {
a[j] = str1[k-cnt];
cnt = cnt - 1;
}
break;
}
else {
cnt++;
//goto one;
}
}
for(m = 0; m <= pos; m++) {
str1++;
}
s2 = atoi(a);
s2 = convertDecimalToBinary(s2);
printf("%d.",s2);
seperate3(str1);
return 0;
}
// function to get third decimal no sepreted
int seperate3(char str1[]) {
int s1, s2, s3, s4;
int i, j, k = 0, m, cnt = 0, cnt1 = 0, pos = 0;
char a[4], str2[100];
for(i = 0; i < strlen(str1); i++) {
pos = cnt;
if(str1[i] == '.') {
k = i;
pos = cnt;
for(j = 0; j < i; j++) {
a[j] = str1[k-cnt];
cnt = cnt - 1;
}
break;
}
else {
cnt++;
//goto one;
}
}
for(m = 0; m <= pos; m++) {
str1++;
}
s3 = atoi(a);
s3 = convertDecimalToBinary(s3);
printf("%d.",s3);
seperate4(str1);
return 0;
}
// function to get fourth decimal no sepreted
int seperate4(char str1[]) {
int s1, s2, s3, s4;
int i, j, k = 0, m, cnt = 0, cnt1 = 0, pos = 0;
char a[4], str2[100];
for(i = 0; i < strlen(str1); i++) {
pos = cnt;
if(str1[i] == '.') {
k = i;
pos = cnt;
for(j = 0; j < i; j++) {
a[j] = str1[k-cnt];
cnt = cnt - 1;
}
break;
}
else {
cnt++;
}
}
for(m = 0; m <= pos; m++) {
str1++;
}
s4 = atoi(a);
s4 = convertDecimalToBinary(s4);
printf("%d\n",s4);
return 0;
}
//to convert decimal to binary
long long convertDecimalToBinary(int n)
{
//printf("%d", n);
long long binaryNumber = 0;
int remainder, i = 1,step=0;
while (n!=0)
{
remainder = n%2;
// printf("Step %d: %d/2, Remainder = %d, Quotient = %d\n", step++, n, remainder, n/2);
n /= 2;
binaryNumber += remainder*i;
i *= 10;
}
return binaryNumber;
}
output:
Enter dotted decimal ip address :
192.15.7.4
Binary Format of IP :
11000000.1111.111.0
I want to convert ip address to binary but,
It always return 0 as binary of last decimal number.
why it is not performing seperate4() function?
What you have done wrong is already listed in the comments but you are doing it overly complicated, nearly Rube-Goldberg like. It is quite simple and can be done without any complicated tricks.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// ALL CHECKS OMMITTED!
char *int8_to_bin(int n, char *buf)
{
int i = 8;
// we need an unsigned integer for the bit-juggling
// because of two's complement numbers.
// Not really needed here because the input is positive
unsigned int N;
// check for proper size
if (n < 0 || n > 255) {
return NULL;
}
// is safe now
N = (unsigned int) n;
// we work backwards here, least significant bit first
// but we want it least significant bit last.
while (i--) {
// make a (character) digit out of an integer
buf[i] = (char) (N & 0x1) + '0';
// shift one bit to the right and
// drop the least significant bit by doing it
N >>= 1;
}
// return the pointer to the buffer we got (for easier use)
return buf;
}
int main(int argc, char **argv)
{
// keeps the intermediate integer
int addr;
// command-line argument, helper for strtol() and int8_to_bin()
char *s, *endptr, *cp;
// buffer for int8_to_bin() to work with
// initialize to all '\0';
char buf[9] = { '\0' };
// array holding the end-result
// four 8-character groups with three periods and one NUL
char binaddr[4 * 8 + 3 + 1];
// iterator
int i;
if (argc < 2) {
fprintf(stderr, "Usage: %s dotted_ipv4\n", argv[0]);
exit(EXIT_FAILURE);
}
// set a pointer pointing to the first argument as a shortcut
s = argv[1];
// the following can be done in a single loop, of course
// strtol() skips leading spaces and parses up to the first non-digit.
// endptr points to that point in the input where strtol() decided
// it to be the first non-digit
addr = (int) strtol(s, &endptr, 0);
// work on copy to check for NULL while keeping the content of buf
// (checking not done here)
cp = int8_to_bin(addr, buf);
// if(cp == NULL)...
// copy the result to the result-array
// cp has a NUL, is a proper C-string
strcpy(binaddr, cp);
// rinse and repeat three times
for (i = 1; i < 4; i++) {
// skip anything between number and period,
// (or use strchr() to do so)
while (*endptr != '.'){
endptr++;
}
// skip the period itself
endptr++;
// add a period to the output
strcat(binaddr, ".");
// get next number
addr = (int) strtol(endptr, &endptr, 0);
cp = int8_to_bin(addr, buf);
// if(cp == NULL)...
strcat(binaddr, cp);
}
printf("INPUT: %s\n", s);
printf("OUTPUT: %s\n", binaddr);
exit(EXIT_SUCCESS);
}
We don't need a complicated parsing algorithm, strtol() does it for us, we just need to find the next period ourselves. The size of all in- and output is known and/or can be easily checked if they are inside their limits--e.g.: no need for tedious and error-prone memory allocations, we can use fixed size buffers and strcat().
There is a principle, that holds not only in the Navy but also in programming: KISS.

How to avoid duplicates when finding all k-length substrings

I want to display all substrings with k letters, one per line, but avoid duplicate substrings. I managed to write to a new string all the k length words with this code:
void subSent(char str[], int k) {
int MaxLe, i, j, h, z = 0, Length, count;
char stOu[1000] = {'\0'};
Length = (int)strlen(str);
MaxLe = maxWordLength(str);
if((k >= 1) && (k <= MaxLe)) {
for(i = 0; i < Length; i++) {
if((int)str[i] == 32) {
j = i = i + 1;
} else {
j = i;
}
for(; (j < i + k) && (Length - i) >= k; j++) {
if((int)str[j] != 32) {
stOu[z] = str[j];
} else {
stOu[z] = str[j + 1];
}
z++;
}
stOu[z] = '\n';
z++;
}
}
}
But I'm struggling with the part that needs to save only one time of a word.
For example, the string HAVE A NICE DAY
and k = 1 it should print:
H
A
V
E
N
I
C
D
Y
Your subSent() routine poses a couple of challenges: first, it neither returns nor prints it's result -- you can only see it in the debugger; second it calls maxWordLength() which you didn't supply.
Although avoiding duplicates can be complicated, in the case of your algorithm, it's not hard to do. Since all your words are fixed length, we can walk the output string with the new word, k letters (plus a newline) at a time, doing strncmp(). In this case the new word is the last word added so we quit when the pointers meet.
I've reworked your code below and added a duplication elimination routine. I didn't know what maxWordLength() does so I just aliased it to strlen() to get things running:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define maxWordLength strlen
// does the last (fixed size) word in string appear previously in string
bool isDuplicate(const char *string, const char *substring, size_t n) {
for (const char *pointer = string; pointer != substring; pointer += (n + 1)) {
if (strncmp(pointer, substring, n) == 0) {
return true;
}
}
return false;
}
void subSent(const char *string, int k, char *output) {
int z = 0;
size_t length = strlen(string);
int maxLength = maxWordLength(string);
if (k >= 1 && k <= maxLength) {
for (int i = 0; i < length - k + 1; i++) {
int start = z; // where does the newly added word begin
for (int j = i; (z - start) < k; j++) {
output[z++] = string[j];
while (string[j + 1] == ' ') {
j++; // assumes leading spaces already dealt with
}
}
output[z++] = '\n';
if (isDuplicate(output, output + start, k)) {
z -= k + 1; // last word added was a duplicate so back it out
}
while (string[i + 1] == ' ') {
i++; // assumes original string doesn't begin with a space
}
}
}
output[z] = '\0'; // properly terminate the string
}
int main() {
char result[1024];
subSent("HAVE A NICE DAY", 1, result);
printf("%s", result);
return 0;
}
I somewhat cleaned up your space avoidance logic but it can be tripped by leading spaces on the input string.
OUTPUT
subSent("HAVE A NICE DAY", 1, result);
H
A
V
E
N
I
C
D
Y
subSent("HAVE A NICE DAY", 2, result);
HA
AV
VE
EA
AN
NI
IC
CE
ED
DA
AY
subSent("HAVE A NICE DAY", 3, result);
HAV
AVE
VEA
EAN
ANI
NIC
ICE
CED
EDA
DAY

Print out the longest substring in c

Suppose that we have a string "11222222345646". So how to print out subsequence 222222 in C.
I have a function here, but I think something incorrect. Can someone correct it for me?
int *longestsubstring(int a[], int n, int *length)
{
int location = 0;
length = 0;
int i, j;
for (i = 0, j = 0; i <= n-1, j < i; i++, j++)
{
if (a[i] != a[j])
{
if (i - j >= *length)
{
*length = i - j;
location = j;
}
j = i;
}
}
return &a[location];
}
Sorry,I don't really understand your question.
I just have a little code,and it can print the longest sub string,hope it can help.
/*breif : print the longest sub string*/
void printLongestSubString(const char * str,int length)
{
if(length <= 0)
return;
int i ;
int num1 = 0,num2 = 0;
int location = 0;
for(i = 0; i< length - 1; ++i)
{
if(str[i] == str[i+1])
++num2;//count the sub string ,may be not the longest,but we should try.
else
{
if(num2 >num1)//I use num1 store the sum longest of current sub string.
{ num1 = num2;location = i - num2;}
else
;//do nothing for short sub string.
num2 = 0;
}
}
for(i = location;str[i]== str[num1];++i)
printf("%c",str[i]);
printf("\n");
}
int main()
{
char * str = "1122222234566";
printLongestSubString(str,13);
return 0;
}
From your code it appears you want to return the longest sub-sequence (sub-string). Since I'm relearning C I thought I would give it a shot.
I've used strndup to extract the substring. I'm not sure how portable it is but I found an implementation if needed, just click on the link. It will allocate memory to store the new cstring so you have to remember to free the memory once finished with the substring. Following your argument list, the length of the sub-string is returned as the third argument of the extraction routine.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *extract_longest_subsequence(const char *str, size_t str_len, size_t *longest_len);
int main()
{
char str[] = "11222234555555564666666";
size_t substr_len = 0;
char *substr = extract_longest_subsequence(str, sizeof(str), &substr_len);
if (!substr)
{
printf("Error: NULL sub-string returned\n");
return 1;
}
printf("original string: %s, length: %zu\n", str, sizeof(str)-1);
printf("Longest sub-string: %s, length: %zu\n", substr, substr_len);
/* Have to remember to free the memory allocated by strndup */
free(substr);
return 0;
}
char *extract_longest_subsequence(const char *str, size_t str_len, size_t *longest_len)
{
if (str == NULL || str_len < 1 || longest_len == NULL)
return NULL;
size_t longest_start = 0;
*longest_len = 0;
size_t curr_len = 1;
size_t i = 0;
for (i = 1; i < str_len; ++i)
{
if (str[i-1] == str[i])
{
++curr_len;
}
else
{
if (curr_len > *longest_len)
{
longest_start = i - curr_len;
*longest_len = curr_len;
}
curr_len = 1;
}
}
/* strndup allocates memory for storing the substring */
return strndup(str + longest_start, *longest_len);
}
It looks like in your loop that j is supposed to be storing where the current "substring" starts, and i is the index of the character that you are currently looking at. In that case, you want to change
for (i = 0, j = 0; i <= n-1, j < i; i++, j++)
to
for (i = 0, j = 0; i <= n-1; i++)
That way, you are using i to store which character you're looking at, and the j = i line will "reset" which string of characters you are checking the length of.
Also, a few other things:
1) length = 0 should be *length = 0. You probably don't actually want to set the pointer to point to address 0x0.
2) That last line would return where your "largest substring" starts, but it doesn't truncate where the characters start to change (i.e. the resulting string isn't necessarily *length long). It can be intentional depending on use case, but figured I'd mention it in case it saves some grief.

Resources