GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 190 of 203    Print  
Count Number of characters using Pointers
Write a program to count the number of characters in a string using pointers.


  
Total Answers and Comments: 7 Last Update: September 29, 2009     Asked by: pulkitgakhar 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
January 01, 2009 04:07:19   #1  
Dhiraj Girdhar Member Since: January 2009   Contribution: 2    

RE: Count Number of characters using Pointers

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.


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 3Overall Rating: -3    
January 02, 2009 00:36:07   #2  
amittyagi24 Member Since: January 2009   Contribution: 1    

RE: Count Number of characters using Pointers
#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;
}

 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
February 12, 2009 20:29:36   #3  
sim_sam Member Since: February 2009   Contribution: 3    

RE: Count Number of characters using Pointers


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.


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
February 16, 2009 00:20:48   #4  
santhosh.kanchanapally Member Since: April 2008   Contribution: 10    

RE: Count Number of characters using Pointers
The below program will give the number of char in a given string

char* str My Name Santhosh ;
int count 0;
while(*str! '') { str++; count++; }
printf( The length of string is : d count);
getchar();

output:
The length of string is : 16

 
Is this answer useful? Yes | No
April 01, 2009 02:28:00   #5  
shanB Member Since: April 2009   Contribution: 1    

RE: Count Number of characters using Pointers
#include
using namespace std;
int main()
{
int count 0;
char *str shannon ;
while(*str ! '')
{
count++;
str++;
}
cout<}

 
Is this answer useful? Yes | No
June 12, 2009 10:46:26   #6  
paindriven Member Since: June 2009   Contribution: 1    

RE: Count Number of characters using Pointers
unsigned int charCounter(char const* str)
{
unsigned int counter 0;
char const* data str;
while (data ! NULL && *data ! ' ')
{
data++;
counter++;
}

return counter;

}

 
Is this answer useful? Yes | No
September 29, 2009 00:39:49   #7  
madhuti Member Since: September 2009   Contribution: 1    

RE: Count Number of characters using Pointers
int main(void) {
char* str "Hellolklklk";
int i 0;
for(i 0; *str; i++ str++);
printf("count dn" i);
return 0;
}

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape