-->





Translate

Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

string class function memchr c example

Leave a Comment
In string class memchr() function helps us to find a byte within a specific memory block. It takes three parameters memchr(buffer, char, size); and returns pointer to first occurrence where char is found else it returns NULL.



Buffer: Memory block in which a byte to be searched
char: Character to search in buffer
size: Size of buffer.

C++ source code: Compiler used CodeBlocks

#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int main ()
{
    char buffer[] = "I like c++ programming";
    cout<<"We have this string -> \n";
    puts(buffer);
    char key;
    char *result;
    cout<<"Enter a character to search in string:  ";
    cin>>key;
    cout<<endl;

    result = (char*)memchr(buffer, key, strlen(buffer));

    if (result!=NULL)
        cout<<key<<" is found at index number:  "<<(result-buffer) ;
     else
        cout<<" NOT FOUND";
    
    cout<<endl;
 return 0;
}


Input output code


program output
string class function memchr c++ example





Read More...

search a character in a string strchr c++ example

1 comment
strchr is a string class function which helps to find a character in a string. It takes two parameters first is string and second is character to search. 
It returns NULL if specified character not found else it returns a pointer value. Remember it is case sensitive "C" and 'c' are different characters for this function

Write a c++ program which takes a string from user and a character then search a character 



C++ program source code


#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
    char stringObj[100], ch;
    char *result = NULL;

    cout<<"\nEnter a String: ";
    gets(stringObj);
    cout<<"\nEnter character to search in string:   ";
    cin>>ch;
    result = strchr(stringObj, ch);
    if(result == NULL) {
         cout<<"\nCharacter ' "<<ch<<" ' NOT FOUND in : "<<stringObj<<endl;
    } else {
         cout<<"\nCharacter ' "<<ch<<" ' FOUND in : "<<stringObj<<endl;
    }
          return 0;
}


program output: 
strchr c++ example

 Program with case sensitive output
strchr c++ example


See also:



Read More...

strncat function c++ example source code

Leave a Comment
strncat function used to concatenate two strings upto specified number of characters. It takes 3 parameters first and second is string third is n where n is number of characters to append first string from second string.
For example
first_string: Hello
second_string: World

strncat( first_string, second_string, 3);

The result will be: Hello Wor

Example program
Write a c++ program which takes 3 inputs from two strings and third is the value of n( the number of characters to append from second string) and display the resultant string.

strncat function C++ source code


#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
    char str1[100],str2[100];

    cout<<"\nEnter first string: ";
    gets(str1);

    cout<<"\nEnter second string: ";
    gets(str2);

    int n;
    cout<<"\nEnter number of characters to concatenat: ";
    cin>>n;
    strncat(str1, str2, n);

    cout<<"\n\n\t\Resultant first String after concatenation: "<<endl;
    cout<<"\n\n\t\t\t"<<str1<<"\n\n\t\t\t";

          return 0;
}


Program output
strncat string class function source code example
  
It is recommended to change the code and examine the output of program.
See Also: strcat function example c++ source code
Read More...

string function strcat c++ example

Leave a Comment
Strcat function comes from the string class with string.h header file. It takes two strings as parameters and return second string append to the first string with putting null character at the end of the string.


Example C++ program 
A C++ program which takes two strings as input from user each of type char and concatenates them with the help of strcat function and displays the result.

See also: concatenate two string without using strcat

strcat example C++ source code

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
    char str1[100],str2[100];

    cout<<"\nEnter first string: ";
    gets(str1);

    cout<<"\nEnter second string: ";
    gets(str2);

    strcat(str1, str2);

    cout<<"\n\n\t\Resultant first String after concatenation: "<<endl;
    cout<<"\n\n\t\t\t"<<str1<<"\n\n\t\t\t";

          return 0;
}


Input Output
strcat example c++ source code

Its a simple program example it is recommended to change the code and examine the output. Happy learning C++
Read More...

concatenate two strings in c++ without using strcat

2 comments
Write a C++ program which take two strings as input from user and concatenate two strings without using the string class function strcat(). Keep the order as first come first and second comes second after concatenation.

This c++ tutorial covers following concepts



  • while loop
  • char array
  • if statement

How c++ program works
  1. There are total 3 strings two for input and third for our result. Each string has fixed size you may change as your requirement. 
  2. There are two index variables both initialized by zero at the beginning of program to get access the index value of both strings
  3.  There are two while loops in the program runs till null not found in both string.
  4. Using index we start read first string from zero index and write its value to resultant string same with the second while loop where we keep increment the index variable value and access second string by the use of index2 variable.
  5. At the end we use for loop variable to show result.


C++ program source code


#include <iostream>
using namespace std;

int main()
{
        int index = 0;
        int index2 = 0;
        const int SIZE = 10;

        char firstStringObj[SIZE], secondStringObj[SIZE], concatString[50]={'0'};

        cout<<"Enter first String: ";
        cin>>firstStringObj;

        cout<<"Enter second String: ";
        cin>>secondStringObj;

        while(firstStringObj[index] != '\0') {
             concatString[index] = firstStringObj[index];
             index++;
            }

        while(secondStringObj[index2] != '\0') {
              index++;
              concatString[index] = secondStringObj[index2];
              index2++;
            }

         cout<<"\n\n\nConcatenated String: ";
            for(int j=0;j<SIZE+SIZE;j++)
                cout<<concatString[j];
         cout<<"\n\n\n";
    return 0;
}

Program output 

concatenate two strings without using strcat string class function



see also:
count number of vowels in arrays

               concatenate two strings using strcat string class function

More examples: C++ programming examples




Read More...

count vowels in a string c++

Leave a Comment
Write a C++ program which counts numbers of vowels in a given string and tell every index where a vowel is found.
This c++ example covers following concepts
How C++ program works
  1. Size of array is fixed using the constant variable. User enters a string then a while loop traverse the whole string till null found.
  2. In while loop using switch statement we check each character of string one by one either it is matching one of case or not. 
  3. When a vowel is found check variable value becomes 1 and if condition becomes true and a vowel is found at x index prints on the console.

C++ Source code

#include <iostream>
using namespace std;

int main()
{
    const int SIZE = 100;
    char stringObj[SIZE];
    int index= 0;
    int valueAt = 0;
    int check = 0;

    cout<<"Enter a String to find vowel:   ";
    cin.getline(stringObj,100);
    while(stringObj[index] !='\0')
    {
        switch(stringObj[index])
        {
          case 'a':
              check = 1;

          case 'e':
              check = 1;

          case 'i':
              check = 1;

          case 'o':
              check = 1;

          case 'u':
              check = 1;

          if(check == 1) {
          cout<<"Vowel "<<stringObj[index]<<" found at index number "<<valueAt<<endl;
          }

        check = 0;

        }
    valueAt++;
    index++;
    }

    return 0;
}


count number of vowels in a string c++


Find more examples here. C++ simple examples
It is recommended make changes in the source code and examine the output.
Read More...

How strings are stored and retrieved in C language

1 comment
String Handling in C programming with common functions
  • String is a combination of characters and it is enclosed in double quotes
  • Character array is used to hold string.
  • Char name_ of_string[ string _ length];
  • Char is data type and string length is the name of characters in a string.
  • A string variable can also be declared without specifying the number of characters.
  • The variables that is used to store a string is called string variables.
  • The last character of a string must be a Null character \0 i.e. Null character \0 is added the end of the string.
  • Null character \0 is also included in the string length.

The string can be initialized
char languageName[20]="cprogramming";
char languageName[ ]   ="cprogramming";

The most common string function are:
  • fputs( )
  • fgets( )
  • strcpy( )
What is fputs() function? Explain with the help of an example.
  • It is used to write a string of characters in to a file
  • The function writes the string until the null character \0 is reached
  • It does not add the null character of the file
  • The file must be opened or in append mode to write this funcation

fputs() Example
Syntax:
fputs(string, file_pointer);

FILE*fp;

Fp = fopen(“test1.txt”,”w”);

fputs(“The string of your choice:”,fp);

The above string is written in test 1.txt file.


What is fgets() funcation? Explain with the help of an example.

  • It is used to read a string of characters of a certain length from a file.
  • The file must be opened or in append mode to write this function.


fgets(string or string variable, n, file _pointer)
N is the number of characters read from the file.

Read More...