What is the problem with that code, I just want to replace the DNA's bases.:
#include <stdio.h>
int getDna(){
int i,t;
printf("How many protein does the DNA have?");
scanf("%d",&t);
char dnaPtn[t],rDnaPtn[t];
printf("Enter the DNA: ");
scanf("%s",&dnaPtn);
for(i=0;i<t;i++){
switch(dnaPtn[i]){
case 'a':
rDnaPtn[i] = "t";
break;
case 'c':
rDnaPtn[i] = "g";
break;
case 'g':
rDnaPtn[i] = "c";
break;
case 't':
rDnaPtn[i] = "a";
break;
default:
break;
}
}
printf("%s",rDnaPtn);
}
int main(){
getDna();
return 0;
}
The output of agcta is:
How many protein does the DNA have: 5
Enter the DNA: agcta
^b'd^!!#
What is wrong?
In your code,
rDnaPtn[i] = "t";
should be
rDnaPtn[i] = 't';
as the "" denotes a string but what you want is of type char.
After that,
don't forget to null-terminate your destination array.
scanf("%s",&dnaPtn); can be re-written as scanf("%s",dnaPtn);
Don't loop over the entire array. make use of strlen().
Enable compiler warnings.
Related
I'm trying to do a simple code but I'm having hard time while trying to make it work.
I want to get an int from stdin between 1 and 50.
if the number is 1 then to print A
if the number is 11 then to print J
if the number is 12 then to print Q
if the number is 13 then to print K
Edit: If its none of them, then just return the number.
i tried to use %c but it wont work for numbers from 10 and above
then i managed to do it by using switch but the default part is not working for me. the only thing i managed to do is to make 50 case's for each number but that's just look horrible.
any help would be appreciated.
#include <stdio.h>
main() {
int number;
scanf("%d", &number);
char* card = NULL;
switch (number)
{
case 1:
card = "A";
break;
case 11:
card = "J";
break;
case 12:
card = "Q";
break;
case 13:
card = "K";
break;
default:
card = //Dont know what to write here//;
}
printf("%s\n", card);
return 0;
}
As far as I can see, this code should work:
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define MAX_STRING_SIZE (sizeof(int)*CHAR_BIT/3 + 3)
int main(int argc, char **argv) {
int number = 0;
scanf("%d", &number);
char card[MAX_STRING_SIZE] = {0};
switch (number)
{
case 1:
strcpy(card, "A");
break;
case 11:
strcpy(card, "J");
break;
case 12:
strcpy(card, "Q");
break;
case 13:
strcpy(card, "K");
break;
default:
sprintf(card, "%d", number);
}
printf("%s\n", card);
return 0;
}
Note that you cannot assign strings with: card = "whatever", you must use strcpy() found in the string.h library. For the default part of your code, I assumed you were trying to convert the card input which was not 1, 11, 12, or 13. In which case, you can use sprintf() to convert the integer value to a string.
I hope this helps!
#define MINCARD 1
#define MAXCARD 50
const char *num2card(int n)
{
static char buf[3];
assert(n >= MINCARD && n <= MAXCARD && "Inval card no.");
switch (num) {
case 1:
return "A";
case 11:
return "J";
case 12:
return "Q";
case 13:
return "K";
default:
snprintf(buf, 3, "%d", num);
}
return buf;
}
Use an array of char. Each numerical "case" will correspond to an index in the array, and the element at that index will be the appropriate "card".
As for what the "default" value should be - that is a requirements question. What is the purpose of this application?
you can try sprintf, but you still have to add in logic for what happens if the number is larger than 50.
#include <stdio.h>
#include <stdlib.h>
int main() {
int number;
scanf("%d", &number);
char* card = malloc(8);
switch (number)
{
case 1:
card = "A";
break;
case 11:
card = "J";
break;
case 12:
card = "Q";
break;
case 13:
card = "K";
break;
default:
sprintf (card , "%i" , number );
}
printf("%s\n", card);
return 0;
}
create function that prompts the user to enter a single character. The return value of the function be a char and will return the character value entered by the user.T his return value will be stored in a local variable, C, in main(). The initial default value of this character will be ' '. The question is how would I store it in the main if the local variable C is not global, and we cannot make it global. I have to use that C variable to finish my other functions. Here is my code:
#include <stdio.h>
#include <stdlib.h>
char enterSingleChar();
int main()
{
char userChoice;
int N = 0;
char C = ' ';
printf("Please choose one of the following choices below \n");
printf("Enter/Change Character (C/c)\n");
printf("Quit Program (Q/q) \n");
scanf("%c", &userChoice);
switch(userChoice)
{
case 'C':
case 'c':
enterSingleChar();
break;
case 'Q':
case 'q':
printf("The program will now quit\n");
exit(1);
default:
break;
}
}
char enterSingleChar()
{
char singleChar = ' ';
printf("Please enter a single character \n");
scanf(" %c", &singleChar);
return singleChar;
}
switch(userChoice)
{
case 'C':
case 'c':
C=enterSingleChar();
break;
case 'Q':
case 'q':
printf("The program will now quit\n");
exit(1);
default:
break;
}
I am a totally newbie about C programming. so my program is very long, sorry.
my professor wants to have a number system- binary to decimal, decimal to binary, octal to decimal, hexadecimal to binary. he also want to have a loop( if he wants to exit press [E], if not then press any key). Now i'm having a problem with this hexadecimal because it keeps saying " type mismatch in redeclaration" and i don't know now how to solve this problem.
so heres my not yet finished program because of "hexadecimal" problem. help me with this error. don't mind the octal to decimal, I am currently programming it :)
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 1000
long num, decimal(long), octal(long), binary(long),j;
char hexadecimal(char), k[MAX];
main()
{
char choice;
clrscr();
printf("[B]inary to Decimal\n");
printf("[D]ecimal to Binary\n");
printf("[O]ctal to Decimal\n");
printf("[H]exadecimal to Binary\n");
printf("[E]xit\n");
printf(" Enter your choice....");
choice=getche();
switch(choice)
{
case 'b':
case 'B': binary(j); break;
case 'd':
case 'D': decimal(num); break;
case 'o':
case 'O':
case 'h':
case 'H': hexadecimal(k[MAX]); break;
case 'e':
case 'E': return 0;
default: printf("\n Invalid choice.... press any key to REPEAT");
getch();
main();
}
printf("\nDo you want to [E]xit?");
choice=getch();
switch(choice)
{
case 'e':
case 'E': printf("\nInvalid choice... press any key to repeat");
getch();
main();
}
getch();
return 0;
}
long binary(long j)
{
long binary_val,decimal_val=0, base=1, rem;
printf("Enter a binary number( 1s & 0s): ");
scanf("%ld",&j);
binary_val=j;
while(j>0)
{
rem=j % 10;
decimal_val=decimal_val + rem * base;
j= j/ 10;
base=base * 2;
}
printf(" The Binary Number is %ld\n",binary_val);
printf(" Its decimal equivalent is = %d\n",decimal_val);
}
long decimal(long num)
{
long decimal_num, remainder, base=1, binary=0;
printf(" \nEnter a decimal integer: ");
scanf("%ld",&num);
decimal_num=num;
while(num>0)
{
remainder= num % 2;
binary=binary + remainder * base;
num=num/2;
base= base * 10;
}
printf(" Input number is %d\n",decimal_num);
printf(" Its binary equivalent is = %ld",binary);
}
char hexadecimal(char k[MAX])
{
long int i=0;
clrscr();
printf(" Enter any Hexadecimal number: ");
scanf("%s",&k);
printf("\n Equivalent binary value: ");
while(k[i])
{
switch(k[i])
{
case '0': printf("0000"); break;
case '1': printf("0001"); break;
case '2': printf("0010"); break;
case '3': printf("0011"); break;
case '4': printf("0100"); break;
case '5': printf("0101"); break;
case '6': printf("0110"); break;
case '7': printf("0111"); break;
case '8': printf("1000"); break;
case '9': printf("1001"); break;
case 'a':
case 'A': printf("1010"); break;
case 'b':
case 'B': printf("1011"); break;
case 'c':
case 'C': printf("1100"); break;
case 'd':
case 'D': printf("1101"); break;
case 'e':
case 'E': printf("1110"); break;
case 'f':
case 'F': printf("1111"); break;
default: printf("\n Invalid hexadecimal digit %c",k[i]); return 0;
}
i++;
}
}
The error you are getting type mismatch in redeclaration of hexadecimalis a result of the difference between the function you prototyped and implemented.
Your prototype is:
char hexadecimal(char), k[MAX];
This line prototypes a function hexadecimal that returns a char and takes a char as an argument AND this line also declares a global char array k of size MAX.
Your actual function is:
char hexadecimal(char k[MAX])
This function is a function that returns a char, but instead of taking a char like your prototype it instead takes a char array of size MAX. As you can see the prototyped function and the function itself are not the same. By making the functions exactly the same you will fix your issue.
To be honest, you don't need to pass anything into that function nor make a global char array as you can locally hold the array based on your code. The only other time you use the array you just pass it to this function which means it is better of as a local to that function anyway. So, you can simply do this:
char hexadecimal(void)
{
char k[MAX]
//same code below...
Now the function takes no arguments and k is still declared in the function, but is local instead of global. The prototype for this function would simply be:
char hexadecimal(void);
I keep on getting a [Linker error]C:\Users etc and collect2: Id returned 1 exit status code errors on my program but I don't see anything wrong with it. This is my program:
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main (){
int vowels = 0, cnsnts = 0;
int i, length;
char string[100];
printf("Enter sentence:");
gets(string);
length = strlen(string);
for(i = 0; i < length; i++){
switch(toUpper(string[i])){
case 'A':
vowels++;
break;
case 'E':
vowels++;
break;
case 'I':
vowels++;
break;
case 'O':
vowels++;
break;
case 'U':
vowels++;
break;
default:
cnsnts++;
}
}
printf("The number of vowels are %d.\n", vowels);
printf("The number of consonants are %d.\n", cnsnts);
system("pause");
return 0;
}
Change
toUpper(string[i])
to
toupper(string[i])
add <ctype.h> header and turn on your compiler warnings.
First add
#include <ctype.h>
then change the camel case toUpper like so
toupper(string[i])
You must #include <ctype.h> and have mistakenly capitalized the u in upper. Your switch could also stand to be brought down a bit
switch(toupper(string[i])) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++;
break;
default:
cnsnts++;
}
This style takes advantage of fall-throughs. If a case doesn't end with a break (or return, continue, or goto) it will enter the case below it. This continues until a control flow altering keyword is hit. The switch above is functionally equivalent to your original, but a lot shorter.
You might also consider checking that string[i] is a letter at all with isalpha before the switch
if (!isalpha(string[i])) continue;
I'm building a program using a grade report and I'm having trouble calculating my GPA using a switch case. I'm unsure why it isn't assigning the correct values. I would also like if there is a way to ask for the number of classes taken and then get the loop to perform that said number of times.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Grades
{
char Name[20];
int Hrs;
int ID;
char ClassName[20];
char Grade;
char ClassID[6];
};
int main ()
{
struct Grades Transcript[6];
int classCnt = 0;
int vHrs=0, vGPA=0, totalHours=0, totalPoints = 0;
char vGrade;
char vName[20], vCID[6], vClassName[20];
printf("Enter Students Name: ");
fgets(vName, 20, stdin);
do
{ printf("\nEnter Class ID: ");
fgets(vCID, 6, stdin);
fflush(stdin);
strcpy_s(Transcript[classCnt].ClassID, vCID);
printf("Enter Class Name: ");
fgets(vClassName, 20, stdin);
strcpy_s(Transcript[classCnt].ClassName, vClassName);
printf("Enter Class Hours: ");
fflush(stdin);
scanf("%d", &vHrs);
Transcript[classCnt].Hrs = vHrs;
printf("Enter Class Grade: ");
fflush(stdin);
scanf("%c", &vGrade);
Transcript[classCnt].Grade = vGrade;
classCnt++;
fflush(stdin);
totalHours+=vHrs;
switch (vGrade) {
case 'A':
case 'a': 4*vHrs;
break;
case 'B':
case 'b': 3*vHrs;
break;
case 'C':
case 'c': 2*vHrs;
break;
case 'D':
case 'd': 1*vHrs;
break;
case 'F':
case 'f': 0;
break;
default: printf("Invalid Grade");}
totalPoints += vGrade;
vGPA = (totalPoints/totalHours);
}while(classCnt<=5);
printf("********************************** Grade Report: *************************************");
printf("\n%d\n", totalHours);
printf("%d\n", vGPA);
system("Pause");
return 0;
The expression statement:
4*vHrs;
is certainly valid in C but it doesn't actually do anything (a).
Perhaps you may want to assign it to something, such as with:
addPoints = 4 * vHrs;
(declaring addPoints beforehand, of course) and then use that to affect totalPoints later:
totalPoints += addPoints;
In terms of asking for a class count, you can use scanf("%d",...) to get an integer from the user and then just use that integer in a loop:
#include <stdio.h>
int main (void) {
int num, count;
printf ("Enter countdown value: ");
scanf ("%d", &count);
for (num = count; num > 0; num--)
printf ("%d ", num);
puts ("BLAST OFF");
return 0;
}
A sample run being:
Enter countdown value: 10
10 9 8 7 6 5 4 3 2 1 BLAST OFF
(a) Even a statement like 42; is valid, though useless. The reason this is allowed is because you can have side effects in expressions. The classic case, though not many learners immediately see this, is the venerable i++;.
This is an expression which gives you the current value of i (which you throw away unless you're using it somehow), then increments i as a side effect.
case 'A':
case 'a': 4*vHrs;
break;
case 'B':
case 'b': 3*vHrs;
break;
case 'C':
case 'c': 2*vHrs;
break;
case 'D':
case 'd': 1*vHrs;
break;
case 'F':
case 'f': 0;
None of these lines have any effect on the program you've written. You might want to assign 3*vHrs, 4*vHrs etc to a variable and then do the calculations below. You probably meant vHrs *= 3 or vHrs *=4 or something like that?