what is strcmp do? strcmp work?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: what is strcmp do? strcmp work?

 
0
  #11
Sep 9th, 2009
Originally Posted by ubi_ct83 View Post
its "a1,b1,c1".
i use const_array as double array so that everytime i just query the row,not the column.
and append_test _data is just a string array.
do u meant with this declaration :
char append_test_data [MAX_LENGTH]="";
i use "" in this array.does it effect?
normally append_test_data[MAX_LENGTH]="";
is just \0 stored in it do you want to make user each time get string from him through the loop and then compare it ? also note that int n=1 that will start from 2nd array count not first since arrays start from 0 could you also post full code to see whats wrong with it
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: what is strcmp do? strcmp work?

 
0
  #12
Sep 9th, 2009
i think something like this would work
  1. for(int n=0;n<=sizeof SzStrings/SzStrings[0];n++)
  2. {
  3. Putrand(append_test_data);
  4. if(strcmp(append_test_data, SzStrings[n]) == 0)
  5. {
  6. printf("Append data is =%s\n",append_test_data);
  7. printf("constraint is found=%s\n", SzStrings[n]);
  8.  
  9. getch();
  10. return 0;
  11.  
  12. }
  13. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 2
Reputation: ronelpadz is an unknown quantity at this point 
Solved Threads: 1
ronelpadz ronelpadz is offline Offline
Newbie Poster

Re: what is strcmp do? strcmp work?

 
0
  #13
Sep 9th, 2009
I like the above discussion. Really. I can really learn from all of you, my masters.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 25
Reputation: ubi_ct83 is an unknown quantity at this point 
Solved Threads: 1
ubi_ct83 ubi_ct83 is offline Offline
Light Poster

Re: what is strcmp do? strcmp work?

 
0
  #14
Sep 10th, 2009
Originally Posted by MrNoob View Post
normally append_test_data[MAX_LENGTH]="";
is just \0 stored in it do you want to make user each time get string from him through the loop and then compare it ? also note that int n=1 that will start from 2nd array count not first since arrays start from 0 could you also post full code to see whats wrong with it
i repeat my prob,
i have an array with 5 row of data and each row has these data:
const_array[0][]=a1,b1,c3,
const_array[1][]=a2,b1,c3,
const_array[2][]=a3,b2,c2,
const_array[3][]=a2,b3,c1,
const_array[4][]=a3,b2,c1,

then i want each of them are compared to this :
char append_test_data[100]="";
for note,the data in this array is always randomly changed but only 1 data each time.so,this means,the const_array just compare to 1 data in append_test_data in a time.

example:
append_test_data =a2,b1,c3, =>the program cant compare them
but if append_test_data=a3,b2,c1, =>only program can compare them.
whats wrong with my program?
  1. if ((in_const = fopen(const_file, "r+")) == NULL)
  2. {
  3. fprintf(stderr, "Cannot open constraint file.\n");
  4. exit(0);
  5. }
  6. char const_array[100][100];
  7.  
  8. int m=0 ;
  9. while(!feof(in_const)& m <=100)
  10.  
  11. {
  12. fgets(const_array[m],100,in_const);
  13. m++;
  14. }
  15.  
  16. for(int n=0;n<=m;n++)
  17. {
  18. if(strcmp(const_array[n],append_test_data) == 0)
  19.  
  20. {
  21. printf("Append data is =%s\n",append_test_data);
  22. printf("constraint is found=%s\n",const_array[n]);
  23.  
  24. getch();
  25. exit(0);
  26. }
  27. }

actually,
i call the file and load to an array in the same function.maybe it effect sumthing.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 3
Reputation: neilkevins is an unknown quantity at this point 
Solved Threads: 1
neilkevins neilkevins is offline Offline
Newbie Poster

Re: what is strcmp do? strcmp work?

 
0
  #15
Sep 10th, 2009
Hi...
I read your information as per that i have some good information on strcmp which i want to share with you....The following error appears: 'strcmp' : cannot convert parameter 1
from 'char' to 'const char *'. I've already tried using single
quotations. the header file
only contains the struct contents. The whole program is part of an
example found in my course work. Does strcmp only compare two sets of
strings or can it be used to determine the end of the string as well?



#include<iostream>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include "program2v.h"

using namespace std;





int main()
{


crecord *string1, *string2, t_str;

*string1 = t_str;

/// the error while( strcmp(*string1->customername, "\0"))
{
*string2 = *string1 + 1;
while( strcmp(*string2->customername, '\0'))
{
if(strcmp( *string1->customername, *string2->customername) > 0 )
{
t_str = *string1;
*string1 = *string2;
*string2 = t_str;

}
string2++;
}
string1++;
}


return 0;
}...
Thanks for sharing the post....
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 25
Reputation: ubi_ct83 is an unknown quantity at this point 
Solved Threads: 1
ubi_ct83 ubi_ct83 is offline Offline
Light Poster

Re: what is strcmp do? strcmp work?

 
0
  #16
Sep 10th, 2009
i found that,the lenght of the strings are different.
strlen (const_array[m])=10,while
strlen (append_test_data)=9.
the funny is,the last data of const_array is 9.thats why its only compile the last data in that array because only the last data has the same lenght with append_test_data.

a1,b1,c3, =strlen (const_array[0])=10
a2,b1,c3, =strlen (const_array[1])=10
a3,b2,c2, =strlen (const_array[2])=10
a2,b3,c1, =strlen (const_array[3])=10
a3,b2,c1, =strlen (const_array[4])=10
a3,b1,c2, =strlen (const_array[5])=9

wow!!!!!

anyone knows how its happens?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 90
Reputation: ajay.krish123 is an unknown quantity at this point 
Solved Threads: 8
ajay.krish123 ajay.krish123 is offline Offline
Junior Poster in Training

Re: what is strcmp do? strcmp work?

 
0
  #17
Sep 10th, 2009
it is used to compare the string letter by letter..if they are same then it return the value zero otherwise the difference in the ascii values in the first letter of the two string which are different.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: what is strcmp do? strcmp work?

 
0
  #18
Sep 10th, 2009
For the OP - if I'm understanding your issue correctly, you are still experiencing problems getting the strcmp() function to return the fact that two strings are equal.

This is happening because the fgets() function stores the newline character into the buffer and then you are comparing this buffer to a string which you think is the same but it doesn't have the '\n' character. You need to overwrite the '\n' character with a NULL.

Because I'm sick of seeing this thread going nowhere (and I've had a good day at work), here's some code for you to try. Here's the "constraint.txt" file I used:

  1. a1,b1,c3,
  2. a2,b1,c3,
  3. a3,b2,c2,
  4. a2,b3,c1,
  5. a3,b2,c1,
Here's the code that reads the above file into the const_array and then tries to find a string that is equal to append_test_data.
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void) {
  6.  
  7. FILE *fp = NULL;
  8. char const_array[100][100];
  9. char append_test_data[100]="a2,b3,c1,";
  10. int ctr= 0, i = 0;
  11. char *p = NULL;
  12.  
  13. // open constraints file
  14. if ((fp = fopen("constraint.txt", "r")) == NULL) {
  15. printf("Cannot open constraint file.\n");
  16. return EXIT_FAILURE;
  17. }
  18.  
  19. // read file and load each line of text into array
  20. // don't forget to overwrite the '\n' that fgets reads into the buffer
  21. while (fgets(const_array[ctr], 100, fp) != NULL) {
  22. if ((p = strchr(const_array[ctr], '\n'))) {
  23. *p = '\0';
  24. }
  25. ctr++;
  26. }
  27.  
  28. // check constraint against append data
  29. for (i = 0; i < ctr; i++) {
  30. if (strcmp(const_array[i], append_test_data) == 0) {
  31. printf("Append data is =%s\n", append_test_data);
  32. printf("constraint is found=%s\n", const_array[i]);
  33. }
  34. }
  35.  
  36. fclose(fp);
  37. return 0;
  38. }
The other issues you had with your original code:

1) You can't declare a variable in the condition statement of a for loop in C.

2) Your use of the feof() function is all wrong. You need to study up on this. In any case it has been removed for the above code snippet.

You also need to start learning how to look up function descriptions in the C function library documentation - if you had done this, you would've been aware of the fgets() function storing the newline character in the buffer.
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: what is strcmp do? strcmp work?

 
0
  #19
Sep 10th, 2009
you can also use strncmp or use strchr to remove new line after each time fgets reads the string
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 25
Reputation: ubi_ct83 is an unknown quantity at this point 
Solved Threads: 1
ubi_ct83 ubi_ct83 is offline Offline
Light Poster

Re: what is strcmp do? strcmp work?

 
0
  #20
Sep 11th, 2009
Originally Posted by yellowSnow View Post
For the OP - if I'm understanding your issue correctly, you are still experiencing problems getting the strcmp() function to return the fact that two strings are equal.

This is happening because the fgets() function stores the newline character into the buffer and then you are comparing this buffer to a string which you think is the same but it doesn't have the '\n' character. You need to overwrite the '\n' character with a NULL.

Because I'm sick of seeing this thread going nowhere (and I've had a good day at work), here's some code for you to try. Here's the "constraint.txt" file I used:

  1. a1,b1,c3,
  2. a2,b1,c3,
  3. a3,b2,c2,
  4. a2,b3,c1,
  5. a3,b2,c1,
Here's the code that reads the above file into the const_array and then tries to find a string that is equal to append_test_data.
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int main(void) {
  6.  
  7. FILE *fp = NULL;
  8. char const_array[100][100];
  9. char append_test_data[100]="a2,b3,c1,";
  10. int ctr= 0, i = 0;
  11. char *p = NULL;
  12.  
  13. // open constraints file
  14. if ((fp = fopen("constraint.txt", "r")) == NULL) {
  15. printf("Cannot open constraint file.\n");
  16. return EXIT_FAILURE;
  17. }
  18.  
  19. // read file and load each line of text into array
  20. // don't forget to overwrite the '\n' that fgets reads into the buffer
  21. while (fgets(const_array[ctr], 100, fp) != NULL) {
  22. if ((p = strchr(const_array[ctr], '\n'))) {
  23. *p = '\0';
  24. }
  25. ctr++;
  26. }
  27.  
  28. // check constraint against append data
  29. for (i = 0; i < ctr; i++) {
  30. if (strcmp(const_array[i], append_test_data) == 0) {
  31. printf("Append data is =%s\n", append_test_data);
  32. printf("constraint is found=%s\n", const_array[i]);
  33. }
  34. }
  35.  
  36. fclose(fp);
  37. return 0;
  38. }
The other issues you had with your original code:

1) You can't declare a variable in the condition statement of a for loop in C.

2) Your use of the feof() function is all wrong. You need to study up on this. In any case it has been removed for the above code snippet.

You also need to start learning how to look up function descriptions in the C function library documentation - if you had done this, you would've been aware of the fgets() function storing the newline character in the buffer.
U r the man!!!!
thanks a lot master...i'll learn more after this..
thanks again
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 960 | Replies: 20
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC