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, ".", ©)))
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, ".");
}
}
Related
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, }
EDIT : Ok,so answer was told by #Mischo5500. Thank you all,guys
How to find out how much numbers are in array except space? My program will find out length of array, until user input will be space and that is the problem. So if I have inserted "10 20 300", the length will be 2. I have expected 3,like three numbers,which were inserted.And I don't know,how much numbers will user type in. I have already tried strlen(p),it was the same thing. Can you help me? Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
float p[1000];
scanf("%f",p);
int length=(sizeof(p)/sizeof(float));
printf("%d",length);
return 0;
}
If i tike it right (sorry if not), you want to calculate number of entered float values. It is easier for you to insert them in string format and convert them to float after processing, if you don't know number of arguments, that will be typed in. Here is how your code can look like for example, float numbers are in val variable, number of inserted values is in length variable.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char p[1000];
float val[1000];
int length = 0;
char* itr;
fgets(p, sizeof(p), stdin);
itr = strtok(p, " ");
while(itr != NULL)
{
val[length] = atof(itr);
itr = strtok(NULL, " ");
length++;
}
printf("%d",length);
return 0;
}
EDIT: If you want "length" of first element, just use strlen() on first token
itr = strtok(p, " \n");
if(itr)
{
length = strlen(itr);
val[length] = atof(itr);
}
You can take in the user inputs in the form of command-line arguments which get passed in to the program as
int main(int argc, char *argv[])
That will give you the entire length of arguments. You would convert each of the arguments argv[i] to numbers.
I'm trying to use array of structure and strtok to track of the frequency of each word in a text. Every time a word is added for the first time to the array set count to 1. If the same word appears again, increment the count. But code can not be executed, the compile passed with out any error and warning. I don't know how to debug the code. the code is shown below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char word[32];
int count;
}Entry;
int main(void) {
int n=0;
Entry entry[n];
char s[]="C (pronounced like the letter C) is a general programming language";
char *delim=" ; ( ) . , \n";
char *p;
p=strtok(s, delim);
printf("%s\n",p);
strcpy(entry[0].word, p);
entry[0].count=1;
while((p!=NULL)){
/*printf("%s",p);*/
while (n<10){
p=strtok(NULL, delim);
if(p==entry[n].word){
entry[n].count++;
}
else{
strcpy(entry[n+1].word, p);
entry[n+1].count=1;
}
n++;
}
}
return 1;
}
if(p==entry[n].word){
You can't compare strings using ==, change to
if(strcmp(p, entry[n].word) == 0)){
And you are reserving space for 0 elements:
int n=0;
Entry entry[n];
I am trying to write a small function to trim left spaces from a string, but I cannot get it right. In this version, I get the following error:
bus error: 10
Could anyone please explain to me what I am doing wrong? I am not looking so much for an alternative piece of code, but would like to understand the errors in my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void trim_string(char *);
int main(int argc, char *argv[]) {
char *temp = " I struggle with strings in C.\n";
trim_string(temp);
printf("%s", temp);
return 0;
}
void trim_string(char *string) {
char *string_trimmed = "";
int i=0, j=0;
while (isblank(string[i])) {
i++;
}
while (string[i] != '\0') {
string_trimmed[j] = string[i];
i++;
j++;
}
string_trimmed[j] = '\0';
strcpy(string, string_trimmed);
}
I have now found a workaround solution, shown below. But I am still not very clear about what I did wrong in the first place:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_LENGTH 100
void trim_string(char [MAX_LENGTH]);
int main(int argc, char *argv[]) {
char temp[MAX_LENGTH] = " I struggle with strings in C.\n";
trim_string(temp);
printf("%s", temp);
return 0;
}
void trim_string(char string[MAX_LENGTH]) {
char string_trimmed[MAX_LENGTH];
int i=0, j=0;
while (isblank(string[i])) {
i++;
}
while (string[i] != '\0') {
string_trimmed[j] = string[i];
i++;
j++;
}
string_trimmed[j] = '\0';
printf("c\n");
strcpy(string, string_trimmed);
}
Both string and string_trimmed point to string literals, here in main:
char *temp = " I struggle with strings in C.\n";
^
|
This is a string literal
temp points to a string literal and the standard says you are not allowed to modify them.
In the function trim_string you are modifying a them which is undefined behavior of which a bus error is one possible result, although anything can happen.
string_trimmed either needs to be an array like this:
char string_trimmed[n] ;
where n is the size of your input using strlen(string) would probably make sense or dynamically allocated via malloc which you would need to free at the end of your function. The same things goes for your input from main, this would work as a substitute:
char temp[] = " I struggle with strings in C.\n";
For completeness sake, the draft C99 standard section 6.4.5 String literals paragraph 6 says (emphasis mine):
It is unspecified whether these arrays are distinct provided their elements have the
appropriate values. If the program attempts to modify such an array, the behavior is
undefined.
I need to split a string and need to store in two seperate variables. The string contains a tab space. so it need to be seperated from the tab space
EG: the string looks like this
Sony <TAB> A Hindi channel.
I need to store Sony in one variable say char a[6]; and A Hindi Channel in another Variable say char b[20];
How can do this?
Tokenize string for a lot of programming language: link
In your case < tab > is a special character and it can be indicated as '\t'.
If you are using C programming language
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
int main(void) {
char *a[5];
const char *s="Sony\tA Hindi channel.";
int n=0, nn;
char *ds=strdup(s);
a[n]=strtok(ds, "\t");
while(a[n] && n<4) a[++n]=strtok(NULL, "\t");
// a[n] holds each token separated with tab
free(ds);
return 0;
}
For C++ without using boost library:
#include <string>
#include <sstream>
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
int main() {
std::string s = "Sony\tA Hindi channel.";
std::vector<std::string> v;
std::istringstream buf(s);
for(std::string token; getline(buf, token, '\t'); )
v.push_back(token);
// elements of v vector holds each token
}
Using C++ and boost: How to tokenize a string in C++
#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;
int main(int, char**) {
string text = "Sony\tA Hindi channel.";
char_separator<char> sep("\t");
tokenizer< char_separator<char> > tokens(text, sep);
BOOST_FOREACH (const string& t, tokens) {
cout << t << "." << endl;
}
}
Probably the strtok function is that you are looking
My C is old but something like that should work:
#include <stdio.h>
int getTabPosition (char str [])
{
int i = 0;
//While we didn t get out of the string
while (i < strlen(str))
{
//Check if we get TAB
if (str[i] == '\t')
//return it s position
return i;
i = i + 1;
}
//If we get out of the string, return the error
return -1;
}
int main () {
int n = 0;
//Source
char str [50] = "";
//First string of the output
char out1 [50] = "";
//Second string of the output
char out2 [50] = "";
scanf(str, "%s");
n = getTabPosition(str);
if (n == -1)
return -1;
//Copy the first part of the string
strncpy(str, out1, n);
//Copy from the end of out1 in str to the end of str
//str[n + 1] to skip the tab
memcpy(str[n+1], out2, strlen(str) - n);
fprintf(stdout, "Original: %s\nout1=%s\nout2=%s", str, out1, out2);
return 0;
}
Untested, but the principe is there