13 Replies - 659 Views - Last Post: 10 January 2013 - 01:47 PM Rate Topic: -----

#1 psteez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-December 12

create string with mix of variable and text and search array for it

Posted 26 December 2012 - 12:11 AM

My program randomly generates a number and keeps track of that number, because it loops and keeps adding those numbers I am basically generating molecules and I need to combine those molecules to search and array of existing amino acids... I have created the array one column has the written on combination of molecules

char names[18][3][20]= {
                        { "Alanine", "C3H7NO2", "89.0929" },
                        { "Aspartic acid", "C4H7NO4", "133.1024" },
                        { "Glutamic acid", "C5H9NO4", "147.1289" },
                        { "Phenylalanine", "C9H11NO2", "165.1887" },
                        { "Glycine", "C2H5NO2", "75.0664" },
                        { "Histidine", "C6H9N3O2", "155.1542" },
                        { "Isoleucine", "C6H13NO2", "131.1724" },
                        { "Lysine", "C6H14N2O2", "146.1870" },
                        { "Leucine", "C6H13NO2", "131.1724" },
                        { "Asparagine", "C4H8N2O3", "132.1176" },
                        { "Proline", "C5H9NO2", "115.1301" },
                        { "Glutamine", "C5H10N2O3", "146.1441" },
                        { "Arginine", "C6H14N4O2", "174.2004" },
                        { "Serine", "C3H7NO3", "105.0923" },
                        { "Threonine", "C4H9NO3", "119.1188" },
                        { "Valine", "C5H11NO2", "117.1459" },
                        { "Tryptophan", "C11H12N2O2", "204.2247" },
                        { "Tyrosine", "C9H11NO3", "181.1881" }
                        };




I don't know how to save the combination of numbers with text to match what I am searching for I have tried
 char *molecule = "C";C;"H";H;"N";N;"O";O; 


Is This A Good Question/Topic? 0
  • +

Replies To: create string with mix of variable and text and search array for it

#2 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 625
  • Joined: 11-June 11

Re: create string with mix of variable and text and search array for it

Posted 26 December 2012 - 12:43 AM

In C, to concatenate C-String and ints, you can use these two ways(there might be other too):

Using itoa & strcat:
Convert the integer value to a C-String using itoa and then concatenate both the C-String with strcat...
Example:
strcat(str,itoa(nVal));    // str is a C-String and nVal is int


Using sprintf:
sprintf works same as printf, but instead of printing the contents it stores it as C-String in the buffer...

Alternatively, if you are using C++, you can easily do your work... By using classes like std::string, std::vector, std::stringstream, to work with strings, store them, concatenate and much more...
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: create string with mix of variable and text and search array for it

Posted 26 December 2012 - 03:46 AM

*
POPULAR

What you really want here is a struct or class, like this:

#include <stdio.h>

typedef struct ntide
{
    const char *name;
    const char *formula;
    double weight;
} Nucleotide;

int main(void)
{
    int i = 0;
    Nucleotide nucleotides[] = {
        { "Alanine", "C3H7NO2", 89.0929 },
        { "Aspartic acid", "C4H7NO4", 133.1024 }
    };
    for(; i < sizeof(nucleotides)/sizeof(nucleotides[0]); ++i)
    {
        printf("%s has a formula of %s and a molecular weight of %.4f\n",
            nucleotides[i].name, nucleotides[i].formula, nucleotides[i].weight);
    }
    return 0;
}

Was This Post Helpful? 6
  • +
  • -

#4 psteez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-December 12

Re: create string with mix of variable and text and search array for it

Posted 08 January 2013 - 04:42 PM

Thank you for the reply s. Is there anyway to do this without using typedef struct ntide.

I was not taught that and I think it's beyond what our teacher whats us to do.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: create string with mix of variable and text and search array for it

Posted 08 January 2013 - 05:02 PM

You also use printf() or sprintf(), but instead of accessing data by element number an field name, you'll have to access data by row and column number.

Using the array of structs is the correct way to do things, though. That 3 dimensional array of characters to hold strings is the old school COBOL/FORTRAN way of storing data.
Was This Post Helpful? 0
  • +
  • -

#6 psteez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-December 12

Re: create string with mix of variable and text and search array for it

Posted 08 January 2013 - 06:40 PM

I am using C
I believe the route I want to go is using sprintf. So the function I am making would store the created amino Acid with all its number of each atom in a buffer as a string and than I would have to see if what stored matches anything in my array.This is what I have so far...


int moleculeFound(float weight, int charge, int c, int h, int o, int n)
{
int i;
int x;
int buffer;
char names[18][3][50] = {
                        { "Alanine", "C3H7NO2", "89.0929" },
                        { "Aspartic acid", "C4H7NO4", "133.1024" },
                        { "Glutamic acid", "C5H9NO4", "147.1289" },
                        { "Phenylalanine", "C9H11NO2", "165.1887" },
                        { "Glycine", "C2H5NO2", "75.0664" },
                        { "Histidine", "C6H9N3O2", "155.1542" },
                        { "Isoleucine", "C6H13NO2", "131.1724" },
                        { "Lysine", "C6H14N2O2", "146.1870" },
                        { "Leucine", "C6H13NO2", "131.1724" },
                        { "Asparagine", "C4H8N2O3", "132.1176" },
                        { "Proline", "C5H9NO2", "115.1301" },
                        { "Glutamine", "C5H10N2O3", "146.1441" },
                        { "Arginine", "C6H14N4O2", "174.2004" },
                        { "Serine", "C3H7NO3", "105.0923" },
                        { "Threonine", "C4H9NO3", "119.1188" },
                        { "Valine", "C5H11NO2", "117.1459" },
                        { "Tryptophan", "C11H12N2O2", "204.2247" },
                        { "Tyrosine", "C9H11NO3", "181.1881" }
                        };

x = sprintf(buffer, "C%dH%dN%dO%d", c, h, n, o);
printf (" the current amino acid is %s\n", x);
}




I am just testing to see if I can actually save the int values to a combined string before I even attempt to search for that string in the array but I am getting compiler errors
"
warning: passing argument 1 of ‚sprintf‚ makes pointer from integer without a cast [enabled by default]
/usr/include/stdio.h:361:12: note: expected ‚char * __restrict__‚ but argument is of type ‚int‚"

Anyone know whats wrong with the way I am making the string??
Thank You so much.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: create string with mix of variable and text and search array for it

Posted 08 January 2013 - 06:44 PM

%d is for printing numbers. %s is for printing strings. You are trying to print strings.
Was This Post Helpful? 1
  • +
  • -

#8 psteez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-December 12

Re: create string with mix of variable and text and search array for it

Posted 08 January 2013 - 06:50 PM

I am trying to make a string from int that are in the main passed to the function. I am so confused :(
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3667
  • View blog
  • Posts: 11,499
  • Joined: 05-May 12

Re: create string with mix of variable and text and search array for it

Posted 08 January 2013 - 06:59 PM

Sorry, I was expecting your c, h, n, and o variables to also be strings since your array stored strings. Anyway, the issue is that buffer has been declared as an int, but you need sprintf() to store data into an array of characters.
Was This Post Helpful? 1
  • +
  • -

#10 psteez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-December 12

Re: create string with mix of variable and text and search array for it

Posted 10 January 2013 - 10:22 AM

That's OK I am getting closer to the resolution... I was able to save what is passed to the function in a string and print it but I want to see if that string is in the second column of my array but it's not working... Here is the code if you could take a look please and offer any advice would be appreciated


int moleculeFound(float weight, int charge, int c, int h, int o, int n)
{
int i;
int x;
char buffer[50];
char names[18][3][50] = {
                        { "Alanine", "C3H7NO2", "89.0929" },
                        { "Aspartic acid", "C4H7NO4", "133.1024" },
                        { "Glutamic acid", "C5H9NO4", "147.1289" },
                        { "Phenylalanine", "C9H11NO2", "165.1887" },
                        { "Glycine", "C2H5NO2", "75.0664" },
                        { "Histidine", "C6H9N3O2", "155.1542" },
                        { "Isoleucine", "C6H13NO2", "131.1724" },
                        { "Lysine", "C6H14N2O2", "146.1870" },
                        { "Leucine", "C6H13NO2", "131.1724" },
                        { "Asparagine", "C4H8N2O3", "132.1176" },
                        { "Proline", "C5H9NO2", "115.1301" },
                        { "Glutamine", "C5H10N2O3", "146.1441" },
                        { "Arginine", "C6H14N4O2", "174.2004" },
                        { "Serine", "C3H7NO3", "105.0923" },
                        { "Threonine", "C4H9NO3", "119.1188" },
                        { "Valine", "C5H11NO2", "117.1459" },
                        { "Tryptophan", "C11H12N2O2", "204.2247" },
                        { "Tyrosine", "C9H11NO3", "181.1881" }
                        };

x = sprintf(buffer, "C%dH%dN%dO%d", c, h, n, o);
printf (" the current amino acid is %s\n", buffer);

 for (i=0; i < 18; i++)
        if (buffer == names[i][i]) {
                x = 1;
                printf("Amino found: %c \n", names[i][0]);
                break;
                return x;
        }
        if (i == 18) {
                x = 0;
                return x;
        }
}



Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: create string with mix of variable and text and search array for it

Posted 10 January 2013 - 11:13 AM

In C you cannot compare arrays (C string is a char array) like this :

31	        if (buffer == names[i][i]) {




You need to use the string compare function strcmp.

The function strcmp returns zero if the strings are equal.

The function is defined in the string.h header.

To access the formula string you need to use 1 in names[j][1].
Was This Post Helpful? 1
  • +
  • -

#12 psteez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-December 12

Re: create string with mix of variable and text and search array for it

Posted 10 January 2013 - 12:54 PM

I am never going to get this
Was This Post Helpful? 0
  • +
  • -

#13 psteez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-December 12

Re: create string with mix of variable and text and search array for it

Posted 10 January 2013 - 01:02 PM

 for (i=0; i < 18; i++) {
        if (strcmp (names[i][1],buffer) == 0) {
                printf("%s\n", names[i]);
                x = 1;
                return x;
        } else
                x = 0;
                return x;
                }


Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: create string with mix of variable and text and search array for it

Posted 10 January 2013 - 01:47 PM

There are three strings on each row you need to specify which :

3         printf("%s\n", names[i]);
          /* should be */
          printf("%s\n", names[i][0]);




In your for loop you are returning when the name is not found in the first row. So let the for loop run :

  /* for each row */
  for (i = 0; i < 18; i++) 
  {
    /* compare the created string to the amino acid string in the array */
    if (strcmp (names[i][1], buffer) == 0 ) 
    {
      printf("%s\n", names[i][0]);

      /* return a symbolic true value */
      return 1;
    }
  }

  /* for loop finished */
  /* return a symbolic false value */
  return 0; 
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1