Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
CPP All : String Handling (Null Terminated)
Concept Learning Code Sheets

Check if string is a palindrome using a function. #1395

Check if a character array or c-style string is same (Palindrome) when reversed using an independent function.

#include <iostream>
#include <string.h>
using namespace std;
bool IsPalindrome(char* str)
{
int len=strlen(str);
for(int i=0;i<len/2;i++)
if(str[i] != str[len-i-1])
return false;
return true;
}
int main()
{
char input[40];
cout<<"Give a string input: ";
cin.getline(input,40);
if(IsPalindrome(input))
cout<<"Given input is a palindrome"<<endl;
else
cout<<"Given input is not a palindrome"<<endl;
return 0;
}

String declaration and initialisation #2545

String declaration as character arrays an initialising them as individual character literals or as string literals.

#include <iostream>
using namespace std;
int main()
{
char s1[3];
s1[0]='C';s1[1]='+';s1[2]='+';
cout<<s1<<endl;

char s2[4];
s2[0]='C';s2[1]='+';s2[2]='+';s2[3]='';
cout<<s2<<endl;

char s3[]="C++";
cout<<s3<<endl;

return 0;
}

Finding string length using the null finding technique #2551

Finding the length of an initialised character array or the c-style string using the null termination check.

#include <iostream>
using namespace std;
int main()
{
char str[]="Programming is magic";
int len=0;
for(int i=0;str[i]!='';i++) len++;
cout<<"String length of ""
<<str<<"" is "<<len<<" characters."<<endl;
return 0;
}

Finding string length using the strlen() function #2556

Finding the length of an initialised character array or the c-style string using the strlen() function.

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[]="Programming is magic";
cout<<"String length of ""
<<str<<"" is "<<strlen(str)<<" characters."<<endl;
return 0;
}

Getting string input from user using cin.getline #2560

Collecting string like input from the user which could be a full line.

#include <iostream>
using namespace std;
int main()
{
char str[80];
cout<<"Input your full name: " ;
cin.getline(str,80);
cout<<"You Entered "<<str;
return 0;
}

Comparing length of two strings entered by the user #2565

A program to compare length of two strings entered by the user using cin.getline() and compared using strlen.

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str1[80],str2[80];
cout<<"Input string 1: " ;
cin.getline(str1,80);
cout<<endl<<"Input string 2: " ;
cin.getline(str2,80);
int a=strlen(str1),b=strlen(str2);
cout<<endl;
if(a>b) cout<<"First string is larger";
else if(b>a) cout<<"Second string is larger";
else cout<<"Both strings are same in size";
cout<<endl;
return 0;
}

Comparing two strings for exact character wise match or for sort order using strcmp() #2571

A program to compare two strings entered by the user if their all characters are same or which string will come first in the sort order using the function strcmp(str1,str2)

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str1[80],str2[80];
cout<<"Input string 1: " ;
cin.getline(str1,80);
cout<<"Input string 2: " ;
cin.getline(str2,80);
int sc=strcmp(str1,str2);
cout<<"strcmp(str1,str2) returns "<<sc<<endl;
if(sc==0)
cout<<"Both strings are same";
else if(sc>0)
cout<<"str1 will come after str2 when ascending";
else if(sc<0)
cout<<"str2 will come after str1 when ascending";
else ;
return 0;
}

gets() puts() string functions use and misuse #2606

Using gets() and puts() string function and its serious limitations

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
char entry1[11];
char entry2[11];
cout<<"Enter some text (max 10 char.): ";
gets(entry1);
cout<<"Enter some text again (max 10 char.): ";
gets(entry2);
puts(entry1);
puts(entry2);
return 0;
}

strcpy() and strcat() use in null terminated strings #2611

Use of functions strcpy() and strcat() on null terminated strings.

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str1[20],str2[20],str3[]=" Cricket";
char str4[50]="";
strcpy(str1,"Play");
strcpy(str2,str3);

strcat(str4,str1);
strcat(str4,str2);
strcat(str4," & Enjoy!");
cout<<str4;
return 0;
}

Reversing a null terminated string #2621

Reversing the original string given by the user and then printing it.

#include <iostream>
#include <string>
using namespace std;
int main()
{
char str[80];
cout<<"Input String to Be Reversed : " ;
cin.getline(str,80);
int len = strlen(str);
char temp;
for(int i=0; i<len/2; i++)
{
temp=str[i];
str[i]=str[len-i-1];
str[len-i-1]=temp;
}
cout <<str<<endl;
return 0;
}


Solved Problems

Comparing two names for sort order #2577

Write a program using the strcmp() function to prove that the name RAKESH wi

First word of sentence (by individual character printing) #2594

Write a program to print the first word of a sentence given by the user.

First word of sentence (by creating another null terminated string) #2601

Write a program to print the first word of a sentence given by the user by cr

count number of spaces in a given sentence #2614

Write a program  to count the number of space characters in a given input st

Change case of string input #2625

Write a program to get input of a string from the user as a character array a

Removing vowels from string #2628

Write a program to collect input sentence from user into a character array as

Word count assuming well formed sentence #2632

Write a program to count words in a given input sentence by a user, assuming

output writing example strcpy, strcat #2649

Write the output of the following program.

Character replacements in a string #2673

Write a c++ program to collect a small sentence from the user and then print

Output Writing – isupper(), islower, isalpha() #2677

Write output of the following program.

Counting words in a text input even if it is not well formed #2717

Write a program to count number of words even when there might be extra space

Find presence of a character in a null terminated string. #2840

Write a program to get a sentence from the user and then a single character f

Reversing each word in a sentence #3073

Write a program to take a sentence input from the user and then print the sen

Frequency of a given character in a sentence #3243

Write a program to input a small sentence from the user and ask about frequen

Quizzes

Code Sheets:10  Solved Problems:14  Quizzes:2
Back