Thursday, April 13, 2017

String and Pointer

Lets learn how to manipulate string using pointer.

This program will find length of given string using pointer.


#include<stdio.h>
 int main() {
   char str[20], *p;
   int cnt=0;
   
   printf("Enter your string : ");
   gets(str);

   p = &str[0];
   
   while(*p != '\0')
   {
    cnt++;
    p++;
   }
   
   printf("The length of \"%s\" is: %d", str, cnt);
   return 0;
}

Output of program

Enter your string : hello students
The length of "hello students" is: 14


No comments:

Post a Comment