Showing posts with label Length. Show all posts
Showing posts with label Length. Show all posts

C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters

How to write a C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters in C Programming Language ?


Solution For C Program :

/*C Program To Find Length Of A String Including Blank Spaces, Tabs, And Other Special Characters.*/

/*New line character will be taken as a string terminating character.*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
int xstrlen(char *);
void main()
{
char s1[20];
int len;
clrscr();
printf("\nEnter the string:");
gets(s1);
len=xstrlen(s1);
printf("\nlength of the string =%d",len);
getch();
}
int xstrlen(char *s)
    {
    int l=0;
    while(*s)
      {
      l++;
      s++;
      }
    return (l);
    }


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program To Find Length Of String And Concatenate Two Strings

How to write a C Program To Find Length Of String And Concatenate Two Strings in C Programming 

Solution For C Program :

/* C program which read two strings and concatenate the largest string to the smallest string (among the two strings).*/

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

int leng(char str[20]);
void concat(char str4[20], char str5[20]);
void main()
{
char str1[20],str2[20],str3[20];
int i,j,len,first,second;
clrscr();
printf("Enter the first string:\n");
gets(str1);

printf("Enter the second string:\n");
gets(str2);
first=leng(str1);
second=leng(str2);
printf("\nLength of 1st string= %d",first);
printf("\nLength of 2nd string= %d",second);

if(first<=second)
    {
    concat(str1,str2);
    }
else
    {
    concat(str2,str1);
    }
getch();
}

int leng(char str[20])
    {
    int i=0;
    while(str[i]!='\0')
        {
        i++;
        }
    return(i);
    }

void concat(char str4[20], char str5[20])
    {
    int i=0,j;
    char str6[20];
    while(str4[i]!='\0')
        {
        str6[i]=str4[i];
        i++;
        }
    j=0;
    while(str5[j]!='\0')
        {
        str6[i]=str5[j];
        i++;j++;
        }
    str6[i]='\0';
    printf("\n\nConcanated string= %s",str6);
    }


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not

How to write a C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not in C Programming Language ?


Solution For C Program :

/*C Program To Find The Length, Reverse Of A String And To Check It Is Palindrome or Not.*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,length=0,flag;
char str[10],rev[10];
clrscr();
printf("Enter the string==>");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
length++;
}
printf("\nLength of %s is ==>%d\n",str,length);
length--;
for(i=0;length>=0;i++)
{
 rev[i]=str[length];
 length--;
}
rev[i]='\0';
printf("\nReverse of %s is ==>%s\n",str,rev);

 length++;
for(i=0;rev[i]!='\0';i++)
{
length++;
}
printf("\nLength of %s is ==>%d\n",rev,length);

for(i=0;str[i]!='\0'||rev[i]!='\0';i++)
{
if(str[i]!=rev[i])
    {
    flag=1;
    break;
    }
}
if(flag==1)
printf("\nString is not polyndrom");
else
printf("\nString is polyndrom");
getch();
}

C Program to calculate Area, Perimeter of Rectangle; Area, Circumference of Circle.


How to write a C Program to calculate Area, Perimeter of Rectangle; Area, Circumference of Circle in C Programming Language ?

Solution:
  1. The program helps you calculate the area and perimeter of a rectangle.
  2. It also calculates the area and the circumference of the circle.
  3. The values of Length, Breadth and Radius are to be entered through the keyboard.
The length and breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area and perimeter of the rectangle, and the area and the circumference of the circle.

/*
aor: area of rectangle
por: perimeter of rectangle
aoc: area of circle
coc: circumference of circle
*/

The circumference of a circle is same as the perimeter that is the distance around the outer edge. 

The formula of circumference is = 2 pi r (pi's symbol) 
where r = the radius of the circle 
and pi = 3.14(Approx)


  1. #include <stdio.h>

  2. //C program to find Area and Circumference of a Circle with Sample Input and Output.

  3. main()
  4. {
  5. float length, breadth, radius, aor, por, aoc, coc;
  6.  
  7. printf ("\nEnter the Length and Breadth of the Rectangle: ");
  8. scanf("%f %f", &length, &amp;breadth);
  9.  
  10. printf("\nEnter the Radius of the Circle: ");
  11. scanf("%f", &radius);
  12.  
  13. aor = length*breadth;
  14. por= 2*(length+breadth);
  15.  
  16.  
  17. aoc = 3.14*radius*radius;
  18.  
  19. coc = 2*radius*3.14;
  20.  
  21. printf("\nThe area of the rectangle is: %f", aor);
  22.  
  23. printf("\nThe perimeter of the rectangle is: %f ", por);
  24.  
  25.  
  26. printf("\n\nThe area of the Circle with radius %f is: %f ", radius, aoc);
  27.  
  28. printf("\nThe circumference of the same circle is: %f", coc);
  29.  
  30. }

C Program to find string length without library function

How to Write a C Program to find string length without library function in C Programming Language ?


Solution:

  1. #include <stdio.h>
  2. #include <string.h>
  3. void main()
  4. {
  5.  int c;
  6.  char a[50];
  7.  printf("Enter A String: ");
  8.  gets(a);
  9.  for(c=0;a[c]!='\0';c++);
  10.  printf("\nYour entertd string is \"%s\" and its Length is \"%d\"",a,c);
  11. }

C Program that prompts the user to input a string, (whitespaces allowed)

How to Write a program that prompts the user to input a string, (whitespaces allowed)
Add code to:
Step 1: allocate memory for the string
Step 2: Scanf the string from the user
Step 3: Write a function to calculate the length of the string.
            int length(char name[]);Â


Solution:

/*Write a program that prompts the user to input a string, (whitespaces allowed)
Add code to:

step1: allocate memory for the string
step2: Scanf the string from the user
step3: Write a function to calculate the length of the string.
int length(char name[]);Â


*/
#include<stdio.h>
#define SIZE 41

int main()
{ 
 //Define student name
 char name[SIZE];
 printf("Please enter your name:");
 scanf("%[^\n]",name);
 
 printf("The length of the name is %d",length(name));
 getch();
}

int length(char name[])
{
 int i,result=0;
 for(i=0;name[i]!='\0';i++)
  result++;
  
 return result;

}