I have a program assignment where one part requires a user-defined function where you enter a string with a minimum length of 2 characters and a maximum length of 20 characters. How would I get it to find the length (I believe should be stored into index) of the entered string s2 with only stdio functions and test to see if it meets the requirements?

void replacementstring(char s2[30], char c, int index)
{
	printf("Enter Replacement String: ");
	gets(s2);
	
	while(index<2 || index>20)
	{
	if (index<2)
		{
			printf("Below the minimum number of characters.");
			gets(s2);
		}
		if (index>20)
		{
			printf("Above the maximum number of characters.");
			gets(s2);
		}	
		
	}

}

First, s2 is not declared large enough to hold 20 characters because it requires one more for the string null-terminating character.

What value is passed into the function for variable index? My guess is either an uninitialized integer or the maximum length of the string.

How to find the lengh of a string without using the functions in string.h? Count them yourself. All you need is a little loop that scans the string to find out where the null terminator character is at.

The posted code also has unterminating while loop as value of index does not change.

See this. You do not want to use gets() . It's dangerous.

And your s2 definition is OK since you have it defined at 30. Dragon misread your code.

And strlen() returns the length of a properly formatted string.

And your s2 definition is OK since you have it defined at 30. Dragon misread your code.

Yes, I read it as 20, not 30.

And strlen() returns the length of a properly formatted string.

But he is not allowed to call any of the functions in string.h. So strlen() can not be used.

And I misread also... It must be catching.

How do you get the length of an entered string using only stdio.h functions? Here's one option:

#include <stdio.h>

int main(void)
{
    char buf[21];
    int n, ch;

    while (scanf("%20[^\n]%n", buf, &n) == 1) {
        /* Extract and discard the trailing newline and any leading extraneous characters */
        do
            ch = getc(stdin); 
        while (ch != '\n' && ch != EOF);

        printf("Read %d characters with value '%s'\n", n, buf);
    }

    return 0;
}

It's a somewhat advanced use of scanf, but with the %[ specifier and a field width you can simulate gets without the buffer overflow risk. The %n specifier tells you how many characters have been extracted up to that point. The only standard functions called are from stdio.h. :)

How do you get the length of an entered string using only stdio.h functions? Here's one option:

#include <stdio.h>

int main(void)
{
    char buf[21];
    int n, ch;

    while (scanf("%20[^\n]%n", buf, &n) == 1) {
        /* Extract and discard the trailing newline and any leading extraneous characters */
        do
            ch = getc(stdin); 
        while (ch != '\n' && ch != EOF);

        printf("Read %d characters with value '%s'\n", n, buf);
    }

    return 0;
}

It's a somewhat advanced use of scanf, but with the %[ specifier and a field width you can simulate gets without the buffer overflow risk. The %n specifier tells you how many characters have been extracted up to that point. The only standard functions called are from stdio.h. :)

Sorry Narue but all that doesn't answer the question about how to get the length of the string.

Sorry Narue but all that doesn't answer the question about how to get the length of the string.

How do you figure? Unless I missed some subtle aspect of the question, the %n specifier does exactly what's needed. A subsequent if statement can verify that the string length is within the desired range.

Ancient One, maybe OP could figure that out and it would not be such a bad thing. Anyway it can be found in zillions of places in net and in K&R (page 36 in first edition).

I do not think OP could convince his teacher having come up with Narue's solution by himself, and probably the teacher recognizes the code from K&R also (unlike me who upvoted K&R word counting style not long ago ;) ) But at least the K&R solution the teacher would understand.

Compliments of 'thinking out of box' (what OP knew to ask) for Narue for posting response to whole program, not OP's question. To know the question sometimes need to know half answer.

Oh nevermind -- just ignore my ignorant comment. Your solution is just too suttle for an old fart like me :)

We are still learning , aren't we?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.