SPACE ALLOCATION OF A STRING IN C

In c every character hold 1 byte space.

H

E

l

L

O

 

w

O

R

l

d

\0

 65505            65506                                                                                                                                             65515           65516

write a program to print a string

#include<stdio.h>

int main()

{

  char name[]=“Geeks with Geeks”;

  int i=0;

  while(i<=15)

{

  printf(“%c”,name[i]);

  i++;

}

  return 0;

}

wap to print String element ending with \o

#include<stdio.h>

int main()

{

  char name[]=“Geeks with Geeks”;

  int i=0;

  while(name[i]=’\o’)

{

  printf(“%c”,name[i]);

  i++;

}

  return 0;

}

WAP to print string element using pointer

#include<stdio.h>

int main()

{

  char name[]=“Geeks with Geeks”;

  char *ptr;

  ptr=name; 

  while(*ptr !=’\o’)

{

  printf(“%c”, *ptr);

  ptr++;

}

  return 0;

}

Here in the example we use as a *ptr to hold character value from name array.

Initial y we allocated space of first element of name array to this ptr variable.

ptr =name;

printf(“%c” , *ptr);

WAP to print string element using %s

#include<stdio.h>

int main()

{

  char name[]=“Geeks with Geeks”;

  printf(“%s” , name);

  return 0;

}

The %s used in print f is a specific format for print out a string .

The some specification can be used receive a string from a keyboard.

write a program

#include<stdio.h>

int main()

{

  char name[25];

  printf(“enter your name”);

  scanf(“%s” , name);

  printf(“hello %s”, name);

  return 0;

}