Articles by "Strings"
Showing posts with label Strings. Show all posts
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Whole India is tweeting about the probable names for Aishwarya Rai's daughter. Some astrologers are suggesting that she will grow up to be a more famous celebrity than Aishwarya if her name is a palindrome. As we all know, a palindrome is a word that can be read the same way in either direction.
Write a C program to determine whether a given word is a palindrome or not. Do not use any string library functions.
Input Format:
Input consists of a single string. Assume that the maximum length of the string is 50.
Sample Input:
Enter the word
hannah
Sample Output:
hannah is a palindrome

Sample Input:
Enter the word
bina
Sample Output:
bina is not a palindrome

Program Code:
#include<stdio.h>
int main()
{
    char str1[50];
    //string str1,str2;
    int length=0;int flag=0; int i=0;
    printf("Enter the word");
    scanf("%s",str1);
    while(str1[i]!='\0')
{
        length++;
        i++;
}
    for(i=0;i<length ;i++)
{
        if(str1[i] != str1[length-i-1])
{
            flag = 1;
            break;
}
}
    if(flag==0)
    printf("\n%s is a palindrome",str1);
    else
    printf("\n%s is not a palindrome",str1);
     return 0;
}
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
A special school is run by an NGO for kids with Dyslexia. As we all know these children will start writing the letters backward or in reverse. Once special care is taken to correct this issue and once they are introduced to words, they will start writing the words in reverse. The teachers do not want to discourage the children at the start itself and they have decided to mark the works written in reverse also as correct. Can you please help the teacher in correcting the answer sheets by writing a C program?
Write a C program to check whether the second word is the reverse of the first word.
Input Format:
Input consists of 2 strings. Assume that the maximum length of the string is 50.

Sample Input:
Enter the actual word
Excellent
Enter the word the student has typed
tnellecxE
Sample Output:
It is correct

Sample Input:
Enter the actual word
Excellent
Enter the word the student has typed
tneilecxE
Sample Output:
It is wrong

Program Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void strrrev(char * str) 
{
int j = 0, i = 0;
while(str[j+1]) j++;
while(i < j) 
{
char temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
int main()
{
    char str1[50],str2[50];
    //string str1,str2;
    //int c;
    printf("Enter the actual word");
    gets(str1);
    printf("\nEnter the word the student has typed");
    gets(str2);
    strrrev(str1);
    if(strcmp(str2,str1)==0)
    //if(c==0)
    printf("\nIt is correct");
    else
    printf("\nIt is wrong");
    return 0;
}
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
In these days kids are introduced to computers at a very early age and in some schools, the dictation test is conducted using computers. The teachers found it a bit difficult to evaluate these tests and they requested the school management to lessen their burden by automating this task. The 12th class students are learning C programming and they took up the task of automating the dictation evaluation. The students have not been introduced to string functions yet. Can you please help them out?
Write a C program to compare 2 strings.
Input Format:
Input consists of 2 strings. Assume that the maximum length of the string is 50 and it contains only alphabets.
Output Format:
Refer sample input and output for formatting specifications.Sample Input and Output 1:
Enter the word
Excellent
Enter the word the student has typed
Excellent
It is correct

Sample Input and Output 2:
Enter the word
Excellent
Enter the word the student has typed
Excelent
It is wrong

Program Code:
#include<stdio.h>

int main()
{
    char str1[50];char str2[50];
    int i=0; int flag=0;
    printf("Enter the word");
    scanf("%s",str1);
    printf("\nEnter the word the student has typed");
    scanf("%s",str2);
    while(str1[i]!='\0' && str2[i]!='\0')
{
         if(str1[i]!=str2[i])
{
             flag=1;
             break;
}
         i++;
    }
    if(flag==0)
    printf("\nIt is correct");
    else
    printf("\nIt is wrong");
    //printf("\nThe number of letters in the name is %d",l);
    return 0;
}
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
In these days kids are introduced to computers at a very early age and in some schools, the dictation test is conducted using computers. The teachers found it a bit difficult to evaluate these tests and they requested the school management to lessen their burden by automating this task. The 12th class students are learning C programming and they took up the task of automating the dictation evaluation. Can you please help them out? 
Write a C program to compare 2 strings using strcmp function.

Sample Input:
Enter the word
Excellent
Enter the word the student has typed
Excellent
Sample Output:
It is correct

Sample Input:
Enter the word
Excellent
Enter the word the student has typed
Excelent
Sample Output:
It is wrong

Program Code:
#include<stdio.h>
#include<string.h>
int main()
{
    char str1[100];char str2[100];
    int i=0; 
    printf("Enter the word");
    scanf("%s",str1);
    printf("\nEnter the word the student has typed");
    scanf("%s",str2);
    i=strcmp(str1,str2);    
    if(i==0)
    printf("\nIt is correct");
    else
    printf("\nIt is wrong");
    //printf("\nThe number of letters in the name is %d",l);
    return 0;
}

no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
The kids need to clear a simple test to secure admission in the nursery class. They need to spell their name and they also need to tell the number of letters in their name. Some of the kids had very long names and the interview panel members found it time-consuming to count the number of letters in the name. One of the panel members has just started to learn programming and she decided to write a C program to count the number of characters in the name. She knew that it was an easy task as the good old strlen function is available in C. Seeing her interest towards programming, another senior panel member who is also running a software concern decided to test her programming skills. He asked her to write the program without using the strlen function. Can you please help her out?
Write a C program to find the number of characters in the string.

Input Format:
Enter the name
Punitha
Output Format:
The number of letters in the name is 7

Program Code:
#include<stdio.h>
int main()
{
    char str[100];
    int i=0; int l=0;
    printf("Enter the name");
    scanf("%s",str);
    while(str[i]!='\0')
    {
        l++;
        i++;
    }
    printf("\nThe number of letters in the name is %d",l);
    return 0;
}
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
The kids need to clear a simple test to secure admission in the nursery class. They need to spell their name and they also need to tell the number of letters in their name. Some of the kids had very long names and the interview panel members found it time-consuming to count the number of letters in the name. One of the panel members has just started to learn to programme and she decided to write a C program to count the number of characters in the name. She knew that it was an easy task as the good old strlen function is available in C. Can you help her out in this task?
Write a C program to find the number of characters in the string.
Input Format:
Input consists of a single string. Assume that the maximum length of the string is 50 and it contains only alphabets.

Enter the name
Punitha
The number of letters in the name is 7

Program Code:
#include<stdio.h>
int main()
{
    char str[100];
    int i=0; int l=0;
    printf("Enter the name");
    scanf("%s",str);
    while(str[i]!='\0')
    {
        l++;
        i++;
    }
    printf("\nThe number of letters in the name is %d",l);
    return 0;
}
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Explain the various string operations with examples.


1. strupr() – This function converts a string to upper case. Syntax: strupr(str1);
Example : strupr(“mukesh rajput”)= MUKESH RAJPUT

2. strlwr() – This function converts a string to lower case. Syntax: strlwr(str1)
Example : strlwr(“CHANDIGARH”)=chandigarh

3. strlen() – This function returns the length of the string. The function returns the number of characters in the strings. Syntax: strlen(str1)

Example :  strlen(“mukesh rajput”)=13

4. strcat()- This function concatenates(combines) two or more stings in to a single string. Syntax: strcat(str1,str2)
Example : x=”muk” y=”esh” strcat(x, y)=mukesh

5. strrev()- This function reverses the string. Syntax: strrev(str1)
Example : strrev(“mukesh”)=hsekum

6. strcpy()- This function copies a string to a variable. Syntax: strcpy(str1,str2)
Example : strcpy(x,”mukesh”) x =mukesh

7. strcmp()- This function compares the two strings and returns the ASCII difference between the two strings. This is case sensitive. Syntax: strcmp(str1,str2)
Example : strcmp(“their”,”there”)=-9


8. strcmpi()- This function compares specified number of characters from both the strings and returns a value. This function is case insensitive.
Example : strcmpi(“There”,”there”)=0



Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Explain the various character functions with example.


1. tolower()- this function convert a character to lower case. 
Example : tolower(‘A’)=a

2. toupper()- this function converts a character to upper case. 

Example : toupper(‘b’)=B

3. toascii()- this function converts character to its ascii equivalent. The output is an integer. 
Example : toascii(‘a’)=65

4. isalpha()- this function checks if a given character is an alphabet or not. 
Example : isalpha(‘a’)=true isalpha(‘*’)=false

5. isdigit()- checks if a given character is a digit or not. 
Example : isdigit(‘8’)= true isdigit(‘I’)=false

6. isspace()- checks if a given character is a white space or not. 
Example : isspace(‘ ‘)=true isspace(‘%’)=false

7. ispunct()- checks if a given character is a punctuation or not. 
Example : ispunct(‘?’)=true ispunct(‘ “)=false

8. islower()-checks if a given character is in lower case or not. 
Example : islower(‘a’)=true islower(‘L’)=false

9. isupper()-checks if a given character is in uppercase or not. 

Example : isupper(‘M’)=true isupper(‘h’)=false



Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Write a C++ program to check that given string is a Palindrome or not.


Program Code:
#include<iostream>
using namespace std;
int main()
{
int i,j,length,flag=1;
char array[20];

cout<<"Enter a string to check for Palindrome : ";
cin>>array;
cout<<endl;
for(length=0;array[length]!='\0';++length);   
for(i=0,j=length-1;i<length/2;++i,--j)
{
if(array[j]!=array[i])
flag=0;
if(flag==1)
{
cout<<"Entered string is a Palindrome.";
}
else
{
cout<<"Entered string is not a Palindrome.";
}
return 0;
}


The program output is tested on www.jdoodle.com
Output:
Enter a string to check for Palindrome :  mukeshsekum
Entered string is a Palindrome.


Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Write a C++ Program to check vowel or a consonant manually in a given string.

Program Code:


#include <iostream>
using namespace std;
int main()
{
char ch;  
int uv, lv;
cout << "Enter your alphabet : "; 
cin >> ch;
// evaluate true if c is a lowercase vowel 
lv = (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u');
// evaluates true if c is an uppercase vowel
uv = (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
// evaluates to true if either ilv or uv is true
if (lv || uv) 
{
cout << ch << " is a vowel.";
}
else   
{  
cout << ch << " is a consonant.";   
}
return 0;
}

The program output is tested on www.jdoodle.com



Output:
Enter your alphabet : g
is a consonant.

Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Write a C++ Program to remove all characters except alphabets in a string.


Program Code:

#include <iostream>
using namespace std;
int main() 
{    
int i;
string name;   
cout << "Enter your string : ";
cout<<endl; 

getline(cin, name);   
for(i = 0; i < line.size(); i++)    
{       
if (!((name[i] >= 'a' && name[i]<='z') || (name[i] >= 'A' && name[i]<='Z')))  
{       
name[i] = '\0';    
}   
} 
cout << "String after removal of all characters except alphabets : " ;
cout<<endl;
cout<< name;    
return 0;
}

The program output is tested on www.jdoodle.com


Outpput:
Enter your string :
Mukesh##$$&&Rajput
String after removal of all characters except alphabets : 
Mukesh       Rajput


Thanks
Mukesh Rajput

no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Write a C++ Program to demonstrate Passing String to a user defined function.

Program Code:


#include <iostream>
using namespace std;
// display() function declaration 
void display(char str[]);
// main() function definition
int main()
{    
char name[30];   
cout << "Enter your string: "; 
cin.get(name, 30); 
// user defined function display() calling 
display(name);   
return 0;
}


// user defined function display() definition
void display(char str[])
{  
cout << "Your entered string is : " ;
cout<< str;
cout<<endl;
}

The program output is tested on www.jdoodle.com

Output:
Enter your string: Mukesh Rajput
Your entered string is : Mukesh Rajput



Thanks
Mukesh Rajput
no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Write a C++ Program to find frequency of characters of a string object.


Program Code:


#include <iostream>
using namespace std;
int main()
{
int i;
int count = 0;
char ch = 'o';
string name = " smart code for you ";
for ( i = 0; i < name.size();  i++)
{
if (name[i] ==  ch)
{
count = count + 1;
}
}
cout << "Number of  character is = " ;
cout<< count;
return 0;
}

The program output is tested on www.jdoodle.com

Output:

Number of  character is = 3



Thanks
Mukesh Rajput

no image
The particular blog is related to C++ programming concepts. OOPs Class Function Operator overloading Inheritance Function Overloading
Write a C++ Program to remove spaces from string entered by programmer.

Program Code:


#include<iostream>
using namespace std;

int main()
{
int i,j=0;
char name[30];
cout<<" Enter your String :  ";;
cout<<endl
cin.get(name, 30);
for(i=0; name[i]!='\0'; i++)
{
if(name[i]!=' ')
{
name[j] = name[i];
j++;
}
}
name[j]='\0';
cout<<" String after removing all spaces : ";
cout<<endl;
cout<< name;
return 0;
}


The program output is tested on www.jdoodle.com

Output:
Enter your String :  
Mukesh Rajput Suruchika
String after removing all spaces : 
MukeshRajputSuruchika



Thanks
Mukesh Rajput