2

I have an apparently easy issue but I just cannot get what am I doing wrong. I've got a code which will test a 60 character entered text in console and if in that text the "terrorist" word appears it will prompt the message "suspect text" and when that word does not appear it would show "nothing suspect". The text entering "mode" should finish when the word "done" is entered. This seems to be my problem because my while loop just does not want to end.

Any hints?

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

int start_with (char *sir1, char *sir2)
{
    int i,j,k;
    int len_sir2=strlen(sir2);
    char sir3[60]="";
    for (i=0;i<=len_sir2;i++)
    {
        sir3[i]=sir1[i];
    }
    k=(strcmp(sir2,sir3)) ? 0:1;
    return k;
}

int main()
{
    char *txt1;
    char sir1[60]="", sir2[60]="terorist", sir_test[60]="done";
    int i,j,lensir1, contor=0,buf_de_la=0, buf_la;  

    while (sir1!=sir_test)
    {
        printf("Enter desired text and press ENTER \n");
        gets(sir1);
        printf("\n");
        buf_la=strlen(sir1);
        char *txt1="Nothing suspect";
        while (buf_de_la<buf_la-7)
        {
            char sirbuf[60]="";
            j=buf_de_la;
            for (i=0;i<=7;i++)
            {
                sirbuf[i]=sir1[j];
                j=j+1;
            }

            if (start_with(sirbuf,sir2)==1)
            {
                txt1="SUSPECT text entered!";
                break;
            }

            buf_de_la=buf_de_la+1;

        }
        printf("%s\n",txt1);
        getch();
        system("cls");
    }

    return 0;


}
user-ag
  • 224
  • 3
  • 20
BogdanM
  • 957
  • 4
  • 15
  • 32
  • 3
    strcmp is your friend – Morb Apr 28 '15 at 13:43
  • But still...i have for my sir1 variable the text "done" loaded (sir[0]="d", sir[1]="o", etc), I can see the value from debug, but strcmp(sir1, "done") or strcmp(sir1,sir_test) returns 0. Any reason why it is not 1? – BogdanM Apr 29 '15 at 05:27

3 Answers3

10

You should compare C-style strings by using strcmp, like this

while (strcmp(sir1, sir_test) != 0)

This happens because when you do sir1!=sir_test, what you are actually testing is if both character arrays are pointing to the same address in memory, which is not true, since they are two different character arrays located at two different memory addresses.

Instead, what you want to do is compare the characters contained in each variables memory space, and that's what strcmp does: it compares character by character until a null terminating character is found.

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
  • thanks for the detailed answer. @al-Acme gave me the hint... – BogdanM Apr 28 '15 at 13:49
  • But still...i have for my sir1 variable the text "done" loaded (sir[0]="d", sir[1]="o", etc), I can see the value from debug, but strcmp(sir1, "done") or strcmp(sir1,sir_test) returns 0. Any reason why it is not 1? – BogdanM 3 mins ago edit – BogdanM Apr 29 '15 at 05:30
  • @BogdanM strcmp returns (taken from documentation): `Zero if lhs and rhs compare equal.`. So you are getting the correct result. – Natan Streppel Apr 29 '15 at 12:08
5

Instead of while (sir1!=sir_test), try this: while (strcmp(sir1,sir_test)). strcmp, strcmpi etc. are the proper functions to be used for string comparisons. They are declared under the string.h header file.

Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
3

The issue is with your comparison while (sir1!=sir_test) which is wrong. It actually compares the addresses of the two strings. You can use strcmp or various other forms of the same function for comparing strings.

A small change which could make your code working is shown below..

do{
        printf("Enter desired text and press ENTER \n");
        .
        .
        .
        system("cls");
}
while (strcmp(sir1,sir_test));
user-ag
  • 224
  • 3
  • 20