Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using a string on `while` in a `do {} while();` loop

I have a problem here. I want to terminate a do while loop in C using a string, but it seemed not to work. I tried putting the identifier outside the do while loop, but the program still goes on. Here is my source code.

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

void getInputAndPatternString(char inputString[100], char pattern[100]);
int substringCounter(char inputString[100], char pattern[100]);

void main()
{
    char inputString[100], pattern[100];
    do
    {
            int counter;

            printf("Pattern Finder\n");

            getInputAndPatternString(inputString, pattern);
            counter = substringCounter(inputString, pattern);

            printf("\n    Number of Patterns Found: %d\n\n", counter);
    }while(inputString != "STOP");
}

void getInputAndPatternString(char inputString[100], char pattern[100])
{
    printf("    Enter Input string: T: ");
    gets(inputString);
    printf("    Enter Pattern string P: ");
    gets(pattern);
}

int substringCounter(char inputString[100], char pattern[100])
{
   int count1, count2=0, same=0, counter=0;
    for(count1=0; count1<=strlen(inputString); count1++)
    {
        if(inputString[count1] == pattern[count2])
        {

            same++;
            count2++;
            if(same == strlen(pattern))
            {
                counter++; 
            }
            else
            {}
        }
        else
        {
            count2 = 0;
            same = 0;
        }
    }
    return counter;
}

When the user inputs ‘STOP’, the program should terminate. Your help guys will be so much appreciated. Thank you!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The following code uses the ==/!= operators on a string! This seems to be a very common confusion for those coming from PHP.

void main()
{
    char inputString[100], pattern[100];
    do
    {
            int counter;

            printf("Pattern Finder\n");

            getInputAndPatternString(inputString, pattern);
            counter = substringCounter(inputString, pattern);

            printf("\n    Number of Patterns Found: %d\n\n", counter);
    } while(inputString != "STOP");
}

Why this doesn’t work?

Because in C, inputString is a pointer to a memory address in the stack. And it contains an integer.

It is same as intptr_t inputString = 0x1234;. And you are trying to compare it with the address of 'S' in "STOP". Why does it compare with the address of 'S', and not with the whole string? A memory address contains a single byte. And 'S' is a single byte in 'STOP', and it is the start of the string. Pointers point to only one byte in memory. Strings are null terminated in C, so, they are represented in this way (0x1, 0x2,.. are example memory addresses):

-------------------------------
| 0x1 | 0x2 | 0x3 | 0x4 | 0x5 |
-------------------------------
|  S  |  T  |  O  |  P  | \0  |
-------------------------------

And strlen() you might’ve seen gives the length of a string, and it works by looping until it finds the \0(null byte) character, and returns the length.

You need to use:

void main()
{
    char inputString[100], pattern[100];
    do
    {
            int counter;

            printf("Pattern Finder\n");

            getInputAndPatternString(inputString, pattern);
            counter = substringCounter(inputString, pattern);

            printf("\n    Number of Patterns Found: %d\n\n", counter);
    } while(strcmp(inputString, "STOP") != 0);
}

This will use strcmp() to compare two strings, and stop if the string "STOP" is found.

Additonal information:

void getInputAndPatternString(char inputString[100], char pattern[100])
{
    printf("    Enter Input string: T: ");
    gets(inputString);
    printf("    Enter Pattern string P: ");
    gets(pattern);
}

gets() is very dangerous to use. Because it gets whatever is in the input buffer, and doesn’t think about the length of the buffer. So, if the buffer is sized 10, and the user enters more than 10 characters, you are going to get a infamous "Segmentation fault" from your OS. Instead, you should use fgets():

void getInputAndPatternString(char inputString[100], char pattern[100])
{
    printf("    Enter Input string: T: ");
    fgets(inputString, 100, stdin);
    inputString[strcspn(inputString, "\n"] = '\0'; // remove newline, because fgets() also includes newline
    printf("    Enter Pattern string P: ");
    fgets(pattern, 100, stdin);
    inputString[strcspn(pattern, "\n"] = '\0'; // remove newline, because fgets() also includes newline
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading