Answers for "string length in c"

C
4

string length c

#include <stdio.h>
#include <string.h>
int main()
{
    char a[20]="Program";
    char b[20]={'P','r','o','g','r','a','m','\0'};

    // using the %zu format specifier to print size_t
    printf("Length of string a = %zu \n",strlen(a));
    printf("Length of string b = %zu \n",strlen(b));

    return 0;
}
Posted by: Guest on October-01-2020
1

string length in c

#include <stdio.h>
// function for string length
int string_length(char str[])
{
    int i;
    // you can use while loop
    for( i = 0; str[i] != '\0';i++);
    return i;
}
int main()
{
    // declaring character array
    char your_name[20]; // you can change the maximum size of string
    printf("Type you full Name : ");
    // taking input from user
    gets(your_name);
    // using string length function
    int length = string_length(your_name);
    // printing string length
    printf("\nLength of your name (%s) is : %d \n\n",your_name,length);

}
Posted by: Guest on June-24-2021
0

how to find length of string in c

// include all the libraries used in the program.
#include <stdio.h>
#include <string.h>

// Calculate Length of String Using strlen() Function
int main() 
{ 
    char a[100]; int length;
	printf("Enter a string to calculate its length\n"); gets(a);
	length = strlen(a);
	printf("Length of the string = %d\n", length);
	return 0; 
}

// Calculate Length of String without Using strlen() Function
int main() 
{
    char s[] = "Programming is fun";
    int i;

    for (i = 0; s[i] != '\0'; ++i);
    
    printf("Length of the string: %d", i);
    return 0;
}
Posted by: Guest on January-05-2021
1

a function to return the length of a string in c

/**
* A PROGRAM THAT RETURNS THE LENGTH OF A STRING FROM THE USER
*/

#include <stdio.h>

/**
* _strlen - takes a string and returns its length
* @i: counter variable
* @*s: the string entered by the user from the terminal
* Return: Length of the string = i
*/
int _strlen(char *s)
{
        int i;

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

        return(i);
}

/**
* main - start of this program
* @str: string entered by user
* Return: 0 when runs successfully
*/
int main()
{
        char str[100];

        printf("Enter your string\n");
        scanf("%s",str);

        printf("%s is %i characters\n", str, _strlen(str));

        return 0;
}
Posted by: Guest on August-04-2021
0

string length program c

#include <stdio.h>
#include <string.h>
int main()
{
  char tab[1000];
  int d = 0;

  printf("enter the string :");
  gets(tab);

  while (tab[d] != '\0'){
    d++;
		}
  for (int i=0;i<1000;i++){
		if(tab[i]==' '){
			d--;
			}
	}
	
  printf(" the length of the string: %d\n", d);

  return 0;
}
Posted by: Guest on September-22-2021

Code answers related to "C"

Browse Popular Code Answers by Language