int const *p declares a 'p' pointer, that points to a constant integer
Here You Are : void String_Concate(char *Arr1, char *Arr2) { int r=0; int c = String_Length(Arr1); int x,p; for(int i=0;Arr1[i];i++) { if((Arr1[i]== 32)(Arr1[i]=='\0')) {r= i; break;} } for(x = r,p =0;x<=c + String_Length(Arr2);x++,p++) { *(Arr1 + x) = *(Arr2 + p); } } Have A nice Day
#include #include voidmain() { int a[10]; int i,sum=0; int *ptr; printf("Enter 10 elements:n"); for(i=0;i
/* with run-time library */ int len; char* str; len = strlen (str); /* without run-time library */ int mystrlen (const char* str) { int len; while (*str != '\0') { len++; str++; } return len; } int len; char* str; len = mystrlen(str);
C-style arrays can be used as data members, both as fixed-size arrays or as dynamic arrays. The following examples demonstrate how they could be used as one-dimensional data members. class a { private: int b[10] {0}; // in-class initialisation (all elements zero). public: int& operator[] (unsigned index) { if (index>=10) throw std::range_error(); return b[index]; } }; class c { public: c (unsigned sz=10): size(sz), p (size?new int[size]:nullptr) { memset (p, 0, size); } c (const s& _s): size(_s.size), p (size?new int[size]:nullptr) { if (size) memcpy (p, _s.p, size); } c (s&& _s): size(_s.size), p (_s.p) { _s.p=nullptr; } // swap resources c& operator= (const s& _s) { if (p) delete [] p; size = _s.size; p = (size?new int[size]:nullptr); if (size) memcpy (p, _s.p, size); } c& operator= (s&& _s) { size = _s.size; p = _s.p; _s.p=nullptr; } // swap resources ~c() { if (p) delete [] p; } int& operator[] (unsigned index) { if (index>=size) throw std::range_error(); return p[index]; } private: unsigned size {0}; int* p {nullptr}; }; A much simpler approach would be to use std::array<int> and std::vector<int> instead of primitive C-style arrays.
I would use a loop like this: const char *p= str-1; size_t count= 0; while (*++p) if (islower (*p)) ++count;
In C++, dynamically allocated variables are best encapsulated in a class. In this way, the variable is automatically deleted when the object falls from scope. #include<iostream> class f () { int* p; public: f (const int i=0): p {new int{i}} {} f (const& f other): p {new int{*other.p;}} {} ~f() {delete p; } f& operator=(const f& other) { *p = *other.p; } f& operator= (const int i) {*p=i;} operator int () { return *p; } }; int main() { { f value = 42; // use value... } // f automatically falls from scope here, releasing the memory allocated to value.p }
#include<iostream> struct point { int x; int y; point (const int _x, const int _y): x {_x}, y {_y} {} double distance (const point&) const; }; double point::distance (const point& p) const { int w = x - p.x; int h = y - p.y; return sqrt (h*h + w*w); } std::ostream& operator<< (std::ostream& os, const point& p) { return os << '{' << p.x << ", " << p.y << '}'; } int main() { using namespace std; point a {5,10}; point b {7,2}; double d = a.distance (b); std::cout << "The distance between coordinates " << a << " and " << b << " is " << d << ".\n" << std::endl; }
Here You Are : void String_Concate(char *Arr1, char *Arr2) { int r=0; int c = String_Length(Arr1); int x,p; for(int i=0;Arr1[i];i++) { if((Arr1[i]== 32)(Arr1[i]=='\0')) {r= i; break;} } for(x = r,p =0;x<=c + String_Length(Arr2);x++,p++) { *(Arr1 + x) = *(Arr2 + p); } } Have A nice Day
What characters do you want to count? As I am not a mind-reader, I can only give you an example which might give you an idea. size_t count_specified_char_occurances (const char *s, int c) { const char *p= s-1; size_t count= 0; while (*++p) if (*p==(char)c) ++count; return count; } int main (void) { printf ("P in APPLE: %d\n", (int)count_specified_char_occurances ("APPLE", 'P'); return 0; }
#include #include voidmain() { int a[10]; int i,sum=0; int *ptr; printf("Enter 10 elements:n"); for(i=0;i
#include <iostream> bool isPrime(int p) {if( p<=1 ( p>2 && p%2==0 ))return( false );int max = (int)sqrt(( double ) p ) + 1;for( int i=3; i<=max; i+=2 )if( p%i==0 )return( false );return( true ); } int main() { const int max=50;int primes[max]; int count=0, num=0; while( count!=max ) {num+=num>2?2:1;if( isPrime(num) )primes[count++]=num;}for( int i=0; i<max; ++i )std::cout << primes[i] << std::endl;return( 0 ); }
const char *p means the char value pointed by 'p' is constant we can't change anyway but the address(location) of 'p' can change. char const *p means the char value pointed by 'p' can change but the location of p can't be change it is constant.
#include <iostream> bool isPrime(int p) {if( p<=1 ( p>2 && p%2==0 ))return( false );int max = (int)sqrt(( double ) p ) + 1; for( int i=3; i<=max; i+=2 )if( p%i==0 )return( false );return( true ); } int main() {for( int i=1; i<5000; i>2?i+=2:++i )if( isPrime(i) )std::cout << i << std::endl;return( 0 ); }
/* with run-time library */ int len; char* str; len = strlen (str); /* without run-time library */ int mystrlen (const char* str) { int len; while (*str != '\0') { len++; str++; } return len; } int len; char* str; len = mystrlen(str);
No need, they certainly are strings in C. The macros of ctype.h (isdigit, isalpha, islower etc) give you more information, e.g: #include <ctype.h> int LettersOnly (const char *p) /* 0/1 = no/yes */ { int reply; for (reply= 1; reply==1 && *p!='\0'; ++p) if (!isalpha(*p)) reply= 0; return reply; }
C-style arrays can be used as data members, both as fixed-size arrays or as dynamic arrays. The following examples demonstrate how they could be used as one-dimensional data members. class a { private: int b[10] {0}; // in-class initialisation (all elements zero). public: int& operator[] (unsigned index) { if (index>=10) throw std::range_error(); return b[index]; } }; class c { public: c (unsigned sz=10): size(sz), p (size?new int[size]:nullptr) { memset (p, 0, size); } c (const s& _s): size(_s.size), p (size?new int[size]:nullptr) { if (size) memcpy (p, _s.p, size); } c (s&& _s): size(_s.size), p (_s.p) { _s.p=nullptr; } // swap resources c& operator= (const s& _s) { if (p) delete [] p; size = _s.size; p = (size?new int[size]:nullptr); if (size) memcpy (p, _s.p, size); } c& operator= (s&& _s) { size = _s.size; p = _s.p; _s.p=nullptr; } // swap resources ~c() { if (p) delete [] p; } int& operator[] (unsigned index) { if (index>=size) throw std::range_error(); return p[index]; } private: unsigned size {0}; int* p {nullptr}; }; A much simpler approach would be to use std::array<int> and std::vector<int> instead of primitive C-style arrays.
#include <iostream.h> #include <conio.h> main() { int num[3],i,j,p,q,tmp,n1,n2,LCM,rem,flag; clrscr(); for(i=0;i<3;i=i+1) { cout<<"Enter No - "<<i+1<<"="; cin>>num[i]; } clrscr(); n1=num[0]; p=n1; for(i=1;i<3;i=i+1) { n2=num[i]; /* Finding HCF */ q=n2; if(p<q) { tmp=q; q=p; p=tmp; } while(p%q!=0) { rem=p%q; q=p; p=rem; if(p<q) { tmp=q; q=p; p=tmp; } } /*finding LCM */ LCM=1; for(j=1;n1%j==0n2%j==0;j=j+1) { if(n1%j==0) { n1=n1/j; flag=1; } if(n2%j==0) { n2=n2/j; flag=1; } if(flag==1) { LCM=LCM*j; } } LCM=LCM*n1*n2; n1=LCM; } cout<<"the LCM ="<<LCM; cout<<"the hcf ="<<q; getch(); } ----- int gcd (int a, int b) { . int tmp; . if (a<0) a= -a; . if (b<0) b= -b; . if (a<b) tmp= a, a= b, b= tmp; . while (b) { . . tmp= a%b; . . a= b; . . b= tmp; . } . return a; } int gcd_n (int n, const int *vect) { . int i, gcdtmp; . for (i=0, gcdtmp=0; i<n && gcdtmp!=1; ++i) . . gcdtmp= gcd (gcdtmp, vect[i]); . return gcdtmp; } int LCM (int a, int b) { . int d= gcd (a, b); . if (d==0) return d; . else return a/d*b; } int lcm_n (int n, const int *vect) { . int i, lcmtmp; . for (i=0, lcmtmp=1; i<n; ++i) . . lcmtmp= LCM (lcmtmp, vect[i]); . return lcmtmp; }