get all indexes of all occurances of a substring in a string - c

i searched online everywhere but i was unable to find a solution that i could implement in my code, i have some limiting factors to take in accounts, the biggest one is: i cannot use pointers to do this, second one is that i cannot edit before the comment
what i have to do is look for the SEC_B sequence in the adn1 string then save the position of it into a int array to then print it something like this:
Found sequence GTC in: 20 62 69 159 167 196
and yes i did count them manually
i have to do the same with the other sequences, but that doesn't matter as long is i can get it working with one, i can then make it work with all the others
so this is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SECUENCIA0 "TGGCGTTTGCAGATTACTGCGTCCCTCACAAGGGTGTGAA"
#define SECUENCIA1 "GCTGTGCATTCGCGGCACAAGAGTCCCGGGTCCCTGTAGC"
#define SECUENCIA2 "TTCACCATCCTGTTGTACCTATCAAACCTACCTACAGCTT"
#define SECUENCIA3 "AGTGAAGGATTATGCGATTGGCGAGCATAGTACCGGCCCG"
#define SECUENCIA4 "TCACACCGTCTCATTGGTGGCCGACCTTGGAACTCCGTCA"
#define SEC_B "GTC"
#define SEC_D "GAT"
#define SEC_K "GT"
int main()
{
char adn1[201];
adn1[0]='\0';
strcat(adn1,SECUENCIA0);
strcat(adn1,SECUENCIA1);
strcat(adn1,SECUENCIA2);
strcat(adn1,SECUENCIA3);
strcat(adn1,SECUENCIA4);
//edit from here
return 0;
}

this solution doesn't involve any explicit pointers (no * anywhere) and doesn't edit the line above the comment, i managed to come up with this solution thanks to the answer of zazz (which he deleted for some reson), here's how i've done it
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SECUENCIA0 "TGGCGTTTGCAGATTACTGCGTCCCTCACAAGGGTGTGAA"
#define SECUENCIA1 "GCTGTGCATTCGCGGCACAAGAGTCCCGGGTCCCTGTAGC"
#define SECUENCIA2 "TTCACCATCCTGTTGTACCTATCAAACCTACCTACAGCTT"
#define SECUENCIA3 "AGTGAAGGATTATGCGATTGGCGAGCATAGTACCGGCCCG"
#define SECUENCIA4 "TCACACCGTCTCATTGGTGGCCGACCTTGGAACTCCGTCA"
#define SEC_B "GTC"
#define SEC_D "GAT"
#define SEC_K "GT"
int main()
{
char adn1[201];
adn1[0]='\0';
strcat(adn1,SECUENCIA0);
strcat(adn1,SECUENCIA1);
strcat(adn1,SECUENCIA2);
strcat(adn1,SECUENCIA3);
strcat(adn1,SECUENCIA4);
//edit below here
printf("---- Búsqueda de secuencias B, D y K dentro del adn1 ------\n");
int PosicionesB[20];
int bs = 0;
int i, l1, l2;
l1 = strlen(adn1);
l2 = strlen(SEC_B);
for(i = 0; i < l1 - l2 + 1; i++) {
if(strstr(adn1 + i, SEC_B) == adn1 + i) {
PosicionesB[bs] = i;
bs++;
i = i + l2 -1;
}
}
printf("Found sequence GTC in:");
for(int i = 0; i < bs;i++){
printf(" %d",PosicionesB[i]);
}
return 0;
}

It's as simple as this:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
void print_indices_substr(const char *str, const char *substr)
{
char *pstr;
printf("Indices: { ");
for (pstr = (char *)str; (pstr = strstr(pstr, substr)) != NULL; pstr = &pstr[1])
printf("%lu, ", (uintptr_t)pstr - (uintptr_t)str);
printf("}\n");
}
Basically, the function strstr returns a pointer to the first occurrence of 'substr' in 'str' or NULL if no occurrence was found. We can use this pointer in a for loop, and the index will be pstr - str, as you can see in the function above. Example:
int main()
{
const char my_string[] = "ABCDABCD";
print_indices_substr(my_string, "ABCD");
return 0;
}
Output:
Indices: { 0, 4, }

Related

How to format a String from "2" to "02" in c

This is my code:
#include <stdio.h>
#include <string.h>
#define VERSION "2.16.0.0"
int main ()
{
//char buf[] ="2.16.0.0";
int i = 0;
int j ;
char letter[8];
//char a[] = VERSION;
for(i=0;i<8;i++)
{
letter[i] = VERSION[i];
}
char *array;
char* copy = letter ;
while ((array = strtok_r(copy, ".", &copy)))
printf("%s\n", array);
printf("%s", array);
}
I split the macro to 2 16 0 0.
Now, I want to format it to 02 16 00 00. How do I do it?
I tried using sprintf() function to format the array but that didn't work out, any other way?
Your program can be simplified in several ways (see below) and I have to point out at least one significant error since the copy of the string in letter does not include the terminating 0.
About how to print, as I understand you would like to print the numerical entries with 2 digits. One method to do that is to convert them to integers and format the output using the printf formatting options:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define VERSION "2.16.0.0"
int main ()
{
char *element;
char copy[] = VERSION;
element = strtok(copy, ".");
while (element != NULL)
{
printf("%02d ", atoi(element));
element = strtok(NULL, ".");
}
}

Storing user-inputted strings into a string array in c?

Fairly new to C, I am trying to read a file of multiple words using bash indirection, and put the words into a string array. The end of the file is marked with a -1.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void init(char* words[]);
int main(int argc,char *argv[]){
char* words[400000];
init(words);
int i = 0;
do{
printf("%s",words[i]);
i++;
}while(!strcmp(words[i],"-1"));
}
void init(char* words[]){ // initializes array
int i = 0;
do{
fgets(words[i],1024,stdin);
i++;
}while(!strcmp(words[i],"-1"));
}
This gives me a segmentation fault, if any other information is needed I'm more than happy to provide it.
If I guessed correctly, '400000' means the max lines the user can input. But the default size of stack on Windows OS is 1M, sizeof(void*) * 400000 = 1,600,000...
The other thing is that you have not allocated memory for every line.
So, I try to correct your code like this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE 4000 // '400000' is really too big!
void init(char* words[]);
int main(int argc,char *argv[]){
char* words[MAX_LINE];
memset(words, 0 , sizeof(words));
init(words);
int i = 0;
do{
printf("%s",words[i]);
delete words[i];
words[i] = nullptr;
i++;
}while(!strcmp(words[i],"-1"));
}
void init(char* words[]){ // initializes array
int maxLen = 1024;
int i = 0;
do{
words[i] = new char[maxLen];
memset(words[i], 0, maxLen);
fgets(words[i], maxLen, stdin);
i++;
}while(!strcmp(words[i],"-1") && i < MAX_LINE);
}

split char data without delimeter

i have this data
27a1bc
thats supposed to be a data recieved from serial communication/uart.
the question is, is there anyway i could separate this data without delimeter? i need the data change to this
27
a1
bc
is there anyway i could do this without delimeter/strtok?
here is my code, im stuck.
#include <stdio.h>
#include <stdlib.h>
char usart[] = "27a1bc";
int main(void) {
// your code goes here
scanf("%c", usart[1]);
scanf("%c", usart[0]);
return 0; }
You can use a pointer to an array of 3 char's (2 + 1 for the trailing NUL) and memcpy in a loop:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char usart[] = "27a1bc";
int main(void)
{
size_t i, n = sizeof usart / 2;
char (*token)[3];
token = calloc(n, sizeof *token);
if (token == NULL) {
perror("calloc");
exit(EXIT_FAILURE);
}
for (i = 0; i < n; i++) {
memcpy(token[i], usart + (i * 2), 2);
puts(token[i]);
}
free(token);
return 0;
}

Randomize letters in string in C

I have for example "asd" and I want it to be randomized to DAS, DSA, SAD, you know. How can I code this? Tried a few solutions but It didnt really work.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
int main()
{
printf("type in the word\n");
char haslo[128];
scanf("%s", haslo);
char set[128];
char hasloa[128];
strcpy(set, haslo);
unsigned int Ind = 0;
srand(time(NULL) + rand());
int len = strlen(set);
while(Ind < len)
{
hasloa[Ind++] = set[rand()%62];
}
hasloa[len] = '\0';
printf("%s", hasloa);
return 0;
}
Change 62 inside the while loop to "len"

Debugger shows wrong value to pointer

I have the following code:
#include <iostream>
#include <cstdio>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <limits>
#include <functional>
#include <algorithm>
#include <cmath>
#include <string>
#include <ostream>
#include <sstream>
#include <bitset>
#include <numeric>
#include<fstream>
using namespace std;
char str[] = "this.is.a.test";
char str2[] = "this.is.another.test";
typedef struct
{
size_t count;
char** strings;
} Tokens;
Tokens Tokenize(char* String, char Split)
{
Tokens t;
t.count = 1;
for (size_t i = 0; String[i] != 0; i++)
{
if (String[i] == Split)
t.count++;
}
t.strings =(char**) malloc(sizeof(char*)* t.count);
if (t.count > 0)
t.strings[0] = String;
for (size_t i = 0, j = 1; String[i] != 0; i++)
{
if (String[i] == Split)
{
t.strings[j] = &String[i + 1];
String[i] = 0;
j++;
}
}
return t;
}
int main(void)
{
Tokens t = Tokenize(str, '.');
printf("number of strings: %i\n---\n", t.count);
for (size_t i = 0; i < t.count; i++)
{
printf("%i: %s\n", i, t.strings[i]);
}
free(t.strings);
}
The problem is when I debug the code and especially that line t.strings[j] = &String[i + 1];
In a test case of this.is.a.test
At the first found dot . , it should points to this, but in the debugger it shows the following picture.
enter code here
What the debugger shows is correct at line 55. The assignment has been made, so t.strings[j] points after the dot.
Note that in Tokenize you allocate Tokens t; on the stack and later return this t. That is bad (very bad!). Because t is on the stack, it will be overwritten by the call to printf.
(And although most is C, formally it is C++ as in C you cannot declare a variable in the for initialization, as in for (size_t i = 0;)

Resources