By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,712 Members | 1,591 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,712 IT Pros & Developers. It's quick & easy.

how to I check a string in "if" loops??

P: n/a
hi
I got a simple program, and I was wondering how do you check if the
string in an array = a string. For example if I put "APPLE" in array
Array[10] then how can I check it with a if statement.

if (Array[ ] == 'APPLE'){
do something;
}

or do I need to use a different method to check?
Thanks
Chris

May 20 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
<ch****@gmail.comwrote:
I got a simple program, and I was wondering how do you check if the
string in an array = a string. For example if I put "APPLE" in array
Array[10] then how can I check it with a if statement.

if (Array[ ] == 'APPLE'){
do something;
}

or do I need to use a different method to check?
That won't work. Use strcmp() in <string.h>
May 20 '07 #2

P: n/a
"ch****@gmail.com" <ch****@gmail.comwrites:
I got a simple program, and I was wondering how do you check if the
string in an array = a string. For example if I put "APPLE" in array
Array[10] then how can I check it with a if statement.
This is in the FAQ.

8.2: I'm checking a string to see if it matches a particular value.
Why isn't this code working?

char *string;
...
if(string == "value") {
/* string matches "value" */
...
}

A: Strings in C are represented as arrays of characters, and C
never manipulates (assigns, compares, etc.) arrays as a whole.
The == operator in the code fragment above compares two pointers
-- the value of the pointer variable string and a pointer to the
string literal "value" -- to see if they are equal, that is, if
they point to the same place. They probably don't, so the
comparison never succeeds.

To compare two strings, you generally use the library function
strcmp():

if(strcmp(string, "value") == 0) {
/* string matches "value" */
...
}

--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
May 20 '07 #3

P: n/a
On May 20, 8:47 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
"chu...@gmail.com" <chu...@gmail.comwrites:
I got a simple program, and I was wondering how do you check if the
string in an array = a string. For example if I put "APPLE" in array
Array[10] then how can I check it with a if statement.

This is in the FAQ.

8.2: I'm checking a string to see if it matches a particular value.
Why isn't this code working?

char *string;
...
if(string == "value") {
/* string matches "value" */
...
}

A: Strings in C are represented as arrays of characters, and C
never manipulates (assigns, compares, etc.) arrays as a whole.
The == operator in the code fragment above compares two pointers
-- the value of the pointer variable string and a pointer to the
string literal "value" -- to see if they are equal, that is, if
they point to the same place. They probably don't, so the
comparison never succeeds.

To compare two strings, you generally use the library function
strcmp():

if(strcmp(string, "value") == 0) {
/* string matches "value" */
...
}

--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Thanks for the help..I corrected my program as you told me, but I get
a small and irritating bug, when I compile the program it says:
trial2-1.c: In function 'co2':
trial2-1.c:18: error: parse error before ']' token

Here is the Source:

#include <stdio.h>

double co2(char fuel[], double amount){
double result;

//Petrol
if (strncmp(fuel[], "Car") == 0){
result = 2.3 * amount;
}

//Oil
else if (strncmp(fuel[], "Oil") == 0){
result = 2.7 * amount;
}

//Coal
else if (strncmp(fuel[], "Coal") == 0){
result = 2.4 * amount;
}

//Wood
else if (strncmp(fuel[], "Wood") == 0){
result = 0 * amount;
}

//Electricity
else if (strncmp(fuel[], "Electricity") == 0){
result = 0.4 * Amount;
}

//Natural Gas
else if (strncmp(fuel[], "Gas") == 0){
result = 0.2 * amount;
}

//Air Travel
else if (strncmp(fuel[], "Flight") == 0){
result = 0.3 * amount;
}

return result;
}
//Main program starts here
int main ()
{
char fuel[20];
double amount;
char desptn[40];
double total=0;
char answer;

FILE * fptr;
fptr = fopen("info.txt", "r");

//Displays the programs function
printf("\n\n");
printf("This program is designed to caculate the net CO2 emissions
\n");
printf("- Using the information listed in a file\n");
printf("- Read and analyse the information\n");
printf("- Give total CO2 emission in kg and 'rations'\n");
printf("- Provide a breakdown of the emissions\n");
printf("- Make use of the 'srncmp' function\n\n");

//Continue program?
printf("Do you what to start the program?(Y/N) ");
scanf(" %c",&answer);
printf("Reading File...\n\n\n");

//If Yes?
if(answer == 'Y' || answer == 'y'){
if (fptr == NULL ){
printf("Cannot Locate File, Please Try again!\n");
printf("Quitting...");
}
while (fscanf(fptr,"%19s %lf %39[^\n]",fuel , &amount, desptn) !=
EOF){
printf("%s \t %.2lf \t %s", fuel, amount, desptn);
printf("\n");
total += co2(fuel,amount);
}
}

printf("The total CO2 emission in kg: %.2lf kg", total);
printf("\n");
printf("Equicalent number of CO2 'rations': %.2lf", total = total /
2500);
printf("\nEnd Program!\n\n");
fclose(fptr);
return 0;
}

Thanks
Chris

May 20 '07 #4

P: n/a
On May 20, 8:47 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
"chu...@gmail.com" <chu...@gmail.comwrites:
I got a simple program, and I was wondering how do you check if the
string in an array = a string. For example if I put "APPLE" in array
Array[10] then how can I check it with a if statement.

This is in the FAQ.

8.2: I'm checking a string to see if it matches a particular value.
Why isn't this code working?

char *string;
...
if(string == "value") {
/* string matches "value" */
...
}

A: Strings in C are represented as arrays of characters, and C
never manipulates (assigns, compares, etc.) arrays as a whole.
The == operator in the code fragment above compares two pointers
-- the value of the pointer variable string and a pointer to the
string literal "value" -- to see if they are equal, that is, if
they point to the same place. They probably don't, so the
comparison never succeeds.

To compare two strings, you generally use the library function
strcmp():

if(strcmp(string, "value") == 0) {
/* string matches "value" */
...
}

--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
Thanks for the help..I corrected my program as you told me, but I get
a small and irritating bug, when I compile the program it says:
trial2-1.c: In function 'co2':
trial2-1.c:18: error: parse error before ']' token

Here is the Source:

#include <stdio.h>

double co2(char fuel[], double amount){
double result;

//Petrol
if (strncmp(fuel[], "Car") == 0){
result = 2.3 * amount;
}

//Oil
else if (strncmp(fuel[], "Oil") == 0){
result = 2.7 * amount;
}

//Coal
else if (strncmp(fuel[], "Coal") == 0){
result = 2.4 * amount;
}

//Wood
else if (strncmp(fuel[], "Wood") == 0){
result = 0 * amount;
}

//Electricity
else if (strncmp(fuel[], "Electricity") == 0){
result = 0.4 * Amount;
}

//Natural Gas
else if (strncmp(fuel[], "Gas") == 0){
result = 0.2 * amount;
}

//Air Travel
else if (strncmp(fuel[], "Flight") == 0){
result = 0.3 * amount;
}

return result;
}
//Main program starts here
int main ()
{
char fuel[20];
double amount;
char desptn[40];
double total=0;
char answer;

FILE * fptr;
fptr = fopen("info.txt", "r");

//Displays the programs function
printf("\n\n");
printf("This program is designed to caculate the net CO2 emissions
\n");
printf("- Using the information listed in a file\n");
printf("- Read and analyse the information\n");
printf("- Give total CO2 emission in kg and 'rations'\n");
printf("- Provide a breakdown of the emissions\n");
printf("- Make use of the 'srncmp' function\n\n");

//Continue program?
printf("Do you what to start the program?(Y/N) ");
scanf(" %c",&answer);
printf("Reading File...\n\n\n");

//If Yes?
if(answer == 'Y' || answer == 'y'){
if (fptr == NULL ){
printf("Cannot Locate File, Please Try again!\n");
printf("Quitting...");
}
while (fscanf(fptr,"%19s %lf %39[^\n]",fuel , &amount, desptn) !=
EOF){
printf("%s \t %.2lf \t %s", fuel, amount, desptn);
printf("\n");
total += co2(fuel,amount);
}
}

printf("The total CO2 emission in kg: %.2lf kg", total);
printf("\n");
printf("Equicalent number of CO2 'rations': %.2lf", total = total /
2500);
printf("\nEnd Program!\n\n");
fclose(fptr);
return 0;
}

Thanks
Chris

May 20 '07 #5

P: n/a
"ch****@gmail.com" <ch****@gmail.comwrites:
Thanks for the help..I corrected my program as you told me, but I get
a small and irritating bug, when I compile the program it says:
trial2-1.c: In function 'co2':
trial2-1.c:18: error: parse error before ']' token

Here is the Source:

#include <stdio.h>
Add #include <string.hhere, at least.
double co2(char fuel[], double amount){
double result;

//Petrol
if (strncmp(fuel[], "Car") == 0){
Use strcmp not strncmp.
Use "fuel" not "fuel[]".
result = 2.3 * amount;
}
[...]
//Air Travel
else if (strncmp(fuel[], "Flight") == 0){
result = 0.3 * amount;
}
What if it isn't any of those?
return result;
}
//Main program starts here
It's really better to avoid silly comments like this.
Everyone knows that the main function is the main program.
int main ()
It's better to write "int main (void)" explicitly.
{
char fuel[20];
double amount;
char desptn[40];
double total=0;
char answer;

FILE * fptr;
fptr = fopen("info.txt", "r");
Should check for success here.
//Continue program?
printf("Do you what to start the program?(Y/N) ");
Do you "what"? :-)
scanf(" %c",&answer);
printf("Reading File...\n\n\n");

//If Yes?
if(answer == 'Y' || answer == 'y'){
if (fptr == NULL ){
printf("Cannot Locate File, Please Try again!\n");
printf("Quitting...");
}
Seems a little late to report that failure here. You knew it
would fail before user interaction started.
printf("Equicalent number of CO2 'rations': %.2lf", total = total /
2500);
It's not very nice for output to have unrelated side effects.
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
May 20 '07 #6

P: n/a
ch****@gmail.com wrote:
>
Thanks for the help..I corrected my program as you told me, but I get
a small and irritating bug, when I compile the program it says:
trial2-1.c: In function 'co2':
trial2-1.c:18: error: parse error before ']' token

Here is the Source:

#include <stdio.h>

double co2(char fuel[], double amount){
double result;

//Petrol
if (strncmp(fuel[], "Car") == 0){
The empty [] after fuel is you problem. Remove them, empty [] are only
valid and required when declaring a function parameter type as an array.

--
Ian Collins.
May 20 '07 #7

P: n/a
Ben Pfaff wrote, On 20/05/07 20:47:
"ch****@gmail.com" <ch****@gmail.comwrites:
>I got a simple program, and I was wondering how do you check if the
string in an array = a string. For example if I put "APPLE" in array
Array[10] then how can I check it with a if statement.

This is in the FAQ.

8.2: I'm checking a string to see if it matches a particular value.
Why isn't this code working?
<snip>

To the OP, the FAQ is located at http://c-faq.com/ and should be checked
before posting questions here.
--
Flash Gordon
May 20 '07 #8

P: n/a
On May 20, 9:09 pm, Flash Gordon <s...@flash-gordon.me.ukwrote:
Ben Pfaff wrote, On 20/05/07 20:47:
"chu...@gmail.com" <chu...@gmail.comwrites:
I got a simple program, and I was wondering how do you check if the
string in an array = a string. For example if I put "APPLE" in array
Array[10] then how can I check it with a if statement.
This is in the FAQ.
8.2: I'm checking a string to see if it matches a particular value.
Why isn't this code working?

<snip>

To the OP, the FAQ is located athttp://c-faq.com/and should be checked
before posting questions here.
--
Flash Gordon
Thank....I got what I needed...I thank you all who answered my
question.

May 20 '07 #9

P: n/a
"ch****@gmail.com" wrote:
>
.... snip ...
>
Thanks for the help..I corrected my program as you told me, but I
get a small and irritating bug, when I compile the program it says:
trial2-1.c: In function 'co2':
trial2-1.c:18: error: parse error before ']' token

Here is the Source:

#include <stdio.h>

double co2(char fuel[], double amount){
double result;

//Petrol
if (strncmp(fuel[], "Car") == 0){
result = 2.3 * amount;
}
.... snip ...
>
//Air Travel
else if (strncmp(fuel[], "Flight") == 0){
result = 0.3 * amount;
}
return result;
}
Use strcmp(fuel, "Car"), not strncmp (which you are miscalling).
Be aware that you may return an undefined result value if no case
matches..

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 21 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.