0
The purpose of the code is to show all substrings of the variable firstString
that has the same size as the second string that is the variable secondString
.
However, when I use the printf("%s\n", pieceOfFirstString);
shows that the string has some strange characters, and I do not know where these characters are coming from (which interferes with other logic of the program), I have read and reread the code several times, probably is something I do not understand yet.
Exceptional cases such as, second string larger than the first and strings of same size have been covered in the original code, this is a minimum verifiable example.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int STRING_MAX_SIZE = 100;
int main()
{
char firstString[STRING_MAX_SIZE];
char secondString[STRING_MAX_SIZE];
scanf("%s\n%s", firstString, secondString);
int sizeFirstString = strlen(firstString);
int sizeSecondString = strlen(secondString);
char pieceOfFirstString[sizeSecondString];
for (int firstCharIndex = 0; sizeFirstString - firstCharIndex >= sizeSecondString; firstCharIndex++)
for (int lastCharIndex = firstCharIndex + 1; lastCharIndex - firstCharIndex != sizeSecondString; lastCharIndex++)
{
int sizeOfPiece = (lastCharIndex - firstCharIndex) + 1;
if (sizeOfPiece == sizeSecondString)
{
strncpy(pieceOfFirstString, firstString + firstCharIndex, sizeOfPiece);
printf("%s\n", pieceOfFirstString);
}
}
system("pause");
return 0;
}
A different piece of code I used and worked out was using argc
and argv
to read strings:
int main(int argc, char *argv[])
{
char* firstString = argv[1];
char* secondString = argv[2];
int sizeFirstString = strlen(firstString);
int sizeSecondString = strlen(secondString);
// (...) Continua lógica do outro pedaço de código.
}
But I want the code to work using scanf();
.
An example of input to test code firstString
, secondString
, in that order, and the outputs, current (with error), ideal (without any kind of error).
Input (For example):
abababa
ab
Output (Current):
Using the scanf();
to read the variables.
ab▒▒b
ba▒▒b
ab▒▒b
ba▒▒b
ab▒▒b
ba▒▒b
Output (Ideal):
Using the argc
and argv
to read the variables.
ab
ba
ab
ba
ab
ba
Summary of the problem: I don’t know why the result of the algorithm changes with the type of reading of the variables firstString
and secondString
, and what I need is that the result of the algorithm using reading with scanf();
is the same as the result of reading with argc
and argv
.
Could you better define what you are trying to do? What are substrings of a string? Permutations of any size? Or does it refer to occurrences of the second string in the first string? In what way? What would it be? I didn’t understand the example nor the argc/argv thing. What does execution parameters have to do with substrings? Is it another way to enter 2 strings into the program? What changes in the program?
– arfneto
I tried to define it better, I hope now to understand. argc/argv was just a different way I know to read the variables, which, I do not know why, worked properly, while using scanf does not work.
– Henrique Hott
If the purpose of the code is to show all substrings of the variable
firstString
that has the same size as the second string that is the variablesecondString
, take a look in this example which is a much simpler option to perform the same task.– Augusto Vasques