Use strlen function to measure the length of user input string

WBOY
Release: 2024-01-25 12:09:07
forward
404 people have browsed it

Use the strlen function to calculate the length of the string entered by the user

char real_word[5][100];

int i,L[5];

printf("

#guess begin

\n");

for (i=0;i

printf("player %d, please enter your word !e79fa5e98193e4b893e5b19e31333332623930\n",i 1);

scanf("%s",real_word[i]) ;

L[i] = strlen(real_word[i]);

printf("%d\n",L[i]);

};

If it is allowed to enter a one-line sentence with spaces, the input sentence can be used:

fgets(real_word[i],100,stdin);

==========================

The complete procedure is as follows

#include

main()

{

char real_word[5][100];

int i,L[5];

printf("

#guess begin

\n");

for (i=0;i

printf("player %d, please enter your word !\n",i 1);

fgets(real_word[i],100,stdin);

L[i] = strlen(real_word[i]);

printf("%d\n",L[i]);

};

Source code of strlen function

The code source is:

int Strlen(const char * const s)

{

int i;Use strlen function to measure the length of user input string

for (i = 0; s[i]; i ) ;

return i;

}

Extended information:

Precautions

Prototype: extern unsigned int strlen(char *s);

Header file: string.h

Format: strlen (character array name)

Function: Calculate the (unsigned int type) length of string s, excluding '\0'

Description: Returns the length of s, excluding the terminator NULL.

For example:

#include

#include

int main(void)

{

char *s="Golden Global View";

printf("%s has %d chars",s,strlen(s));

getchar();

return 0;

}

What does strlen mean in C language

strlen: The name of the library function for calculating the length of a string.

str: ​​Usually, programmers like to use it as string variable name. It is string (abbreviation of the English word string).

len: Usually, programmers like to use it as variable name. It is length (an abbreviation for the English word length).

For example:

char str[20]="I am a student";Use strlen function to measure the length of user input string

int len;

len = strlen(str);

printf("the string length is: %d",len);

Extended information:

Function prototype

extern unsigned int strlen(char *s);

In Visual C 6.0 or Dev-C, the prototype is size_t strlen(const char *string);, where size_t is actually unsigned int. You can see such code in VC6.0 or Dev-C:

typedef unsigned int size_t; header file: string.h or cstring

Format: strlen (character pointer expression)

Function: Calculate the length of the given string (unsigned int type), excluding '\0'

Description: Returns the length of s, excluding the terminator NULL.

strlen(char*) function is the actual length of the string. It is obtained from the beginning to the first '\0'. If you only define it without assigning an initial value to it, the result is uncertain. , it will search from the first address of aa until it encounters '\0' and stops.

Reference source: Encyclopedia-strlen (C/C language function)

Call the strlen function to sort the string array and put it into the W array

There are too many questions to explain. You should just look at my code

#include "stdio.h"

#include "string.h"

main()

{char a[5][10], w[5][10];

char t[10];

int i , j ;

for(i=0;igets(a[i]);

for(i=4;i>0;i--)

for(j=0;jif(strlen(a[j])>strlen(a[j 1]))

{strcpy(t,a[j 1]); strcpy(a[j 1],a[j]); strcpy(a[j],t); }

for(i=0;i{

strcpy(w[i],a[i]);

puts(w[i]);

}

}

In short, your mistakes are:

1.t type is wrong

###2. There is a 1 written as i### ###3. A semicolon is added directly after the if statement### ###4. Part of strcpy(w[i],a[i]); code is completely redundant### ###5.for(i=4;i>=0;i) should be i--###

The above is the detailed content of Use strlen function to measure the length of user input string. For more information, please follow other related articles on the PHP Chinese website!

source:docexcel.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template