5 Replies - 350 Views - Last Post: 22 January 2013 - 08:29 PM Rate Topic: -----

#1 smfsher47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-January 13

Program to check a string sequence having individual character values

Posted 22 January 2013 - 08:01 AM

I want to create a program which will read a string sequence say AATC upto four characters and calculate the total sum of the values held by each characters. For example if the sequence starts with A the value of A will be 2 if the first letter is G then the value of G is 4 like this. The program will check the string see the first letter and calculate the value until it reaches the end of the string which is the fourth letter and calculate the overall sum. Here is my code.

#include <stdio.h>
#include <conio.h>
void main(void)
{    char name[4];
    int i,l,s;
    printf("Enter Seq");
    scanf("%c",name[i]);
    clrscr();
    for(i=0; i<4 ;i++)

    {
{if(name[1]=='a')
l=2;
{if(name[1]=='t')
l=-2;
{if(name[1]=='g')
l=5;
{if(name[1]=='c')
l=-1;
}
s=l;
}

printf("Score is %d",s);
     getch();
}



I am not getting an error but then again I am not getting my result. Please tell me What is the problem in my algorithm or Program.

Is This A Good Question/Topic? 0
  • +

Replies To: Program to check a string sequence having individual character values

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Program to check a string sequence having individual character values

Posted 22 January 2013 - 08:19 AM

Quote

I am not getting an error

That is quite strange since there are quite a few errors in the code you posted.

Here are the error messages I received when I compiled your program.

Quote

||=== c_homework, Debug ===|
main.c|3|error: return type of ‘main’ is not ‘int’ [-Wmain]|
main.c||In function ‘main’:|
main.c|7|warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]|
main.c|26|error: expected declaration or statement at end of input|
main.c|26|error: expected declaration or statement at end of input|
main.c|26|error: expected declaration or statement at end of input|
||=== Build finished: 4 errors, 1 warnings ===|


You seem to have mismatched braces, and several other problems.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 smfsher47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-January 13

Re: Program to check a string sequence having individual character values

Posted 22 January 2013 - 10:14 AM

View Postjimblumberg, on 22 January 2013 - 08:19 AM, said:

Quote

I am not getting an error

That is quite strange since there are quite a few errors in the code you posted.

Here are the error messages I received when I compiled your program.

Quote

||=== c_homework, Debug ===|
main.c|3|error: return type of ‘main’ is not ‘int’ [-Wmain]|
main.c||In function ‘main’:|
main.c|7|warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]|
main.c|26|error: expected declaration or statement at end of input|
main.c|26|error: expected declaration or statement at end of input|
main.c|26|error: expected declaration or statement at end of input|
||=== Build finished: 4 errors, 1 warnings ===|


You seem to have mismatched braces, and several other problems.

Jim


sorry I forgot the braces but here is the updated one

#include <stdio.h>
#include <conio.h>
void main(void)
{    char name[4];
    int i,l,m,n,p,s;
    printf("Enter Seq");
    scanf("%c",name[i]);
    clrscr();
    for(i=0; i<4 ;i++)

    {
{if(name[1]=='a')
l=2;
}
{if(name[1]=='t')
m=-2;
}
{if(name[1]=='g')
n=5;
}
{if(name[1]=='c')
p=-1;
}
s=l+m+n+p;
}

printf("Score is %d",s);
     getch();
}


Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3105
  • Posts: 10,903
  • Joined: 08-August 08

Re: Program to check a string sequence having individual character values

Posted 22 January 2013 - 10:32 AM

Indent!!!
And it's int main(), not void.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Program to check a string sequence having individual character values

Posted 22 January 2013 - 11:11 AM

You need to assign values to your variables before you use them. The following will probably cause a segmentation fault if you don't assign a value to i. Also remember that a char can hold one and only one character.
    scanf("%c",name[i]);

You probably want to put this inside a loop to get all four values.

I really think you should readup on how to use if statements, you have your braces in what looks like improper locations. Normally if statements look like:
if(statement)
{
   // Do stuff.
}
else
{
   // Do something else.
}

Notice the placement of the braces. Is that how you have placed your braces?

Jim
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Program to check a string sequence having individual character values

Posted 22 January 2013 - 08:29 PM

scanf("%c",name[i]);



scanf accepts a pointer as it's input so it can modify the memory at that location. You are passing the value of name[i]

This post has been edited by jjl: 22 January 2013 - 08:29 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1