C++ Tutorials

C++ Tutorials

Print char array in C++

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main(){
//Print char array

char arr[100]="apple";
int i=0;
while(arr[i] != '\0'){
  cout<<arr[i]<<endl;
  i++;
}

return 0 ;
}

Print char in an array from a user using C++

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main(){
//Print char in array from user

char arr[100];
cout<<"Enter a char: ";
cin>>arr;
cout<<"Your char is: "<<arr;

return 0 ;
}

Print particular char in an array from a user using C++

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main(){
// Print perticular char in array from user

char arr[100];
cout<<"Enter a char: ";
cin>>arr;
cout<<"Your 2 char is: "<<arr[2]<<endl;
cout<<"Your 3 char is: "<<arr[3];

return 0 ;
}

Check Palindrome char in an array from a user using C++

//Solution 1:
#include<bits/stdc++.h>
#include<iostream>
using namespace std;

bool isPalindrome(char arr[]){
  int i =0;
  int j = strlen(arr) - 1;
  while(i<j){
    if(arr[i] == arr[j]){
      i++;
      j--;
    }
    else{
      return false;
    }
  }
  return true;
}

int main(){

//Check palimdrome char in array from user
char arr[1000];
cout<<"Enter any word: ";
cin.getline(arr, 1000);
if(isPalindrome(arr)){
  cout<<arr<<" is Palindrome word";
}
else{
  cout<<arr<<" Not a palindrome word";
}


return 0 ;
}
//Solution 2:
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main(){

//Check palimdrome char in array from user

int n;
cout<<"Enter a char array size: ";
cin>>n;
char arr[n+1];
cout<<"Enter a char: ";
cin>>arr;
bool check =1;
for (int i=0; i<n; i++){
  if(arr[i] != arr[n-1-i]){
    check=0;
    break;
  }
}
if(check == true){
  cout<<arr<<" is a palindrome word."<<endl;
}
else{
  cout<<arr<<" not a palindrome word.";
}

Reading sentence in an array using C++


#include<bits/stdc++.h>
#include<iostream>

using namespace std;
//Reading sentence in a array
void readLine(char arr[], int maxLen){
  int i=0;
  char ch = cin.get();
  while(ch != '\n'){
    arr[i] = ch;
    i++;
    if(i == (maxLen-1)){
      break;
    }
    ch = cin.get();

  }
  arr[i]='\0';
  return;
}
int main(){
char arr[1000];
readLine(arr, 1000);
cout<<arr<<endl;

return 0 ;
}

Reading paragraph in an array using C++

#include<bits/stdc++.h>
#include<iostream>

using namespace std;
//Reading sentence in a array
void readLine(char arr[], int maxLen, char delim = '\n'){
  int i=0;
  char ch = cin.get();
  while(ch != delim){
    arr[i] = ch;
    i++;
    if(i == (maxLen-1)){
      break;
    }
    ch = cin.get();

  }
  arr[i]='\0';
  return;
}
int main(){
char arr[1000];
readLine(arr, 1000, '$');
cout<<arr<<endl;

return 0 ;
}
Output: Type your paragraph and last used $ symbol for end paragraph.

Remove consecutive duplicate characters from a string using C++

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
//Ccoooding --> Coding
void removeDuplicate(char arr[]){
  int l = strlen(arr);
  if(l == 1 || l == 0){
    return;
  }
  int preview = 0;
  for(int current = 1; current < l; current ++){
    if(arr[current] != arr[preview]){
      preview++;
      arr[preview] = arr[current];
    }
  }
  arr[preview + 1] = '\0';
  return;

}

int main(){

//Remove consecutive duplicate characters from a string
char arr[1000];
cout<<"Enter any word: ";
cin.getline(arr, 1000);
removeDuplicate(arr);
cout<<arr<<endl;

return 0 ;
}

Print n number of a paragraph from a user using C++ string

#include<bits/stdc++.h>
#include<cstring>
#include<iostream>
using namespace std;

int main(){

//Print n number of paragraph
int n;
cout<<"Enter number paragraph: ";
cin>>n;
char a[1000];
cin.get();
for(int i=0; i< n; i++){
  cin.getline(a, 1000);
  cout<<a<<endl;
}

return 0 ;
}