Count Number of characters using Pointers

Write a program to count the number of characters in a string using pointers.

Questions by pulkitgakhar

Showing Answers 1 - 7 of 7 Answers

int CharCount(char *str)
{
     int count = 0;
  
     if (str != NULL) { 
         for (; *(str + count) != ''; ++count);

     }

     return count;
}

Here function CharCount will return the number of characters in string.

#include<iostream.h>

void main()
{
    
int Char_Count(char *get_string);

char *strcount = "THIS IS A STRING";

int int_word_count  = Char_Count(strcount);

cout<<"word count is = "<<int_word_count;
    
}

int Char_Count(char *get_string)
{
    int count = 0;
    
    if (get_string != NULL) {
        for (;*(get_string+count) != '';++count);
        
    }
    return count;
    }

  Was this answer useful?  Yes

sim_sam

  • Feb 12th, 2009
 



const char* A= "my name is sam";

char* pPointer= A; // set the pointer to point to the first element of the string
int counter=0;


// until the element addressed by pPointer is not null,                     
//traverse the string and increment the counter

while (*pointer= '')
{
pointer++;
counter++;
}

When while exits, counter contains the number of characters in the string.

  Was this answer useful?  Yes

shanB

  • Apr 1st, 2009
 

#include
using namespace std;
int main()
{
??????? int count=0;
??????? char *str="shannon";
??????? while(*str !='')
??????? {
??????????????? count++;
??????????????? str++;
??????? }
??????? cout<}

  Was this answer useful?  Yes

paindriven

  • Jun 12th, 2009
 

unsigned int charCounter(char const* str)
{
unsigned int counter = 0;
char const* data = str;
while (data != NULL && *data != '')
{
data++;
counter++;
}

return counter;

}

  Was this answer useful?  Yes

madhuti

  • Sep 29th, 2009
 

int main(void) {
    char* str = "Hellolklklk";
    int i=0;
    for(i=0; *str; i++, str++);
    printf("count= %dn", i);
return 0;
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions