Answers for "string copy in c++"

12

erasing a character from a string in c++

// string::erase
#include <iostream>
#include <string>

int main ()
{
  std::string str ("This is an example sentence.");
  std::cout << str << '\n';
                                           // "This is an example sentence."
  str.erase (10,8);                        //            ^^^^^^^^
  std::cout << str << '\n';
                                           // "This is an sentence."
  str.erase (str.begin()+9);               //           ^
  std::cout << str << '\n';
                                           // "This is a sentence."
  str.erase (str.begin()+5, str.end()-9);  //       ^^^^^
  std::cout << str << '\n';
                                           // "This sentence."
  return 0;
}
Posted by: Guest on May-13-2020
0

copy string in c++

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define sg struct gammal
struct gammal {
    char name[20];
    int payment;
    struct gammal *next;
};
void add (sg *g){
    char name[20];
    int payment;
    printf("Enter Name: ");
    scanf("%s",name);
    printf("Enter Payment: ");
    scanf("%d",&payment);
    if(g->payment == -1){
        strcpy(g->name,name);
        g->payment = payment;
        g->next = NULL;
    }
    else {
        while(g->next != NULL)
            g = g->next;
        g->next = (sg*)malloc(sizeof(sg));
        g = g->next;
        strcpy( g->name,name);
        g->payment = payment;
        g->next = NULL;
    }
}
void show (sg *g){
    while ( g != NULL){
        printf("---------");
        printf("%s ",g->name);
        printf("%d ",g->payment);
        g = g->next;
    }
}
int main()
{
    sg *g =(sg*)malloc(sizeof(sg));
    g->payment = -1;
    add(g);
    add(g);
    show(g);
}
Posted by: Guest on August-04-2021
0

string copy in cpp

//use '=' to copy string
string s1={"Hello"};
string s2;
s2=s1;	//now s2 will have a copu s1
Posted by: Guest on March-23-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language