Found 1401 Articles for C

Find the Longest Substring Containing Vowels in Even Counts in C++

Arnab Chakraborty
Updated on 29-Apr-2020 13:12:13

298 Views

Suppose we have the string s, we have to find the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times. So if the string is like “helloworld”, then the output will be 8.To solve this, we will follow these steps −ret := 0, define two maps m and cnt, set m[“00000”] := -1store vowels into vowels arrayfor i in range 0 to size of sx := s[i], and ok := falseincrease cnt[x] by 1, set temp := empty stringfor k in ... Read More

Coroutines in C/C++

Ayush Gupta
Updated on 16-Mar-2020 10:14:42

128 Views

In this tutorial, we will be discussing a program to understand coroutines in C/C++.Coroutines are control instructions which switch the execution control between two routines which returning any of them.Example Live Demo#include int range(int a, int b){    static long long int i;    static int state = 0;    switch (state){    case 0:       state = 1;       for (i = a; i < b; i++){          return i;       //returning control       case 1:; //resuming control       }    }    state = 0;    return 0; } int main(){    int i;    for (; i=range(1, 5);)       printf("control at main :%d", i);    return 0; }Outputcontrol at main :1 control at main :2 control at main :3 control at main :4

Creating a C/C++ Code Formatting tool with help of Clang tools

Ayush Gupta
Updated on 16-Mar-2020 10:11:22

145 Views

In this tutorial, we will be discussing a program to create a C/C++ code formatting tool with the help of clang tools.SETUPsudo apt install python sudo apt install clang-format-3.5Then we will create a python file in location where the current user has read and write permissions.Exampleimport os cpp_extensions = (".cxx", ".cpp", ".c", ".hxx", ".hh", ".cc", ".hpp") for root, dirs, files in os.walk(os.getcwd()):    for file in files:       if file.endswith(cpp_extensions):          os.system("clang-format-3.5 -i -style=file " + root + "/" + file)Create a file formatting file in the top directory of the current user.Outputclang-format-3.5 -style=google -dump-config ... Read More

Create Directory or Folder with C/C++ Program

Ayush Gupta
Updated on 16-Mar-2020 10:07:19

2K+ Views

In this tutorial, we will be discussing a program to create directory or folder with C/C++ program.To create a new directory we will be using the mkdir() command. Note that the given code will work only for windows compiler.Example#include #include #include #include void main(){    int check;    char* dirname = "tutorialspoint";    clrscr();    check = mkdir(dirname);    //checking if directory is created    if (!check)       printf("Directory created");    else {       printf("Unable to create directory");       exit(1);    }    getch();    system("dir/p");    getch(); }OutputDirectory created

Core Dump (Segmentation fault) in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:54:10

4K+ Views

In this tutorial, we will be discussing a program to understand core dump (segmentation fault) in C/C++.It happens due to reasons like when code tries to write on read only memory or tries to access corrupt memory location.ExampleModifying a string literalint main(){    char *str;    str = "GfG";    *(str+1) = 'n';    return 0; }Accessing out of array index bounds#include using namespace std; int main(){    int arr[2];    arr[3] = 10;    return 0; }Accessing an address which is freed#include #include int main(void){    int* p = malloc(8);    *p = 100;    free(p);    *p = 110;    return 0; }OutputAbnormal termination of program

Converting Strings to Numbers in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:52:05

185 Views

In this tutorial, we will be discussing a program to understand how to convert strings into numbers in C/C++.C/C++ provides two ways to convert strings into numbers.Example Live DemoUsing sscanf()#include int main(){    const char *str = "12345";    int x;    sscanf(str, "%d", &x);    printf("The value of x : %d", x);    return 0; }OutputThe value of x : 12345Using stoi() Live Demo#include #include using namespace std; int main(){    string str1 = "45";    string str2 = "3.14159";    string str3 = "31337 geek";    int myint1 = stoi(str1);    int myint2 = stoi(str2);    int myint3 = stoi(str3);    cout

Convert a String to Integer Array in C/C++

Ayush Gupta
Updated on 16-Mar-2020 09:50:25

3K+ Views

In this tutorial, we will be discussing a program to understand how to convert a string into integer array in C/C++.For this we will create a new array. Traverse through the given string, if the character is a comma “, ”, we move on to the next character else add it to the new array.Example Live Demo#include using namespace std; //converting string to integer array void convert_array(string str){    int str_length = str.length();    int arr[str_length] = { 0 };    int j = 0, i, sum = 0;    //traversing the string    for (i = 0; str[i] != ... Read More

Thread functions in C/C++

Ayush Gupta
Updated on 02-Mar-2020 11:16:59

2K+ Views

In this tutorial, we will be discussing a program to understand thread functions in C/C++.Thread functions allow users to implement concurrent functions at the same time, which can either be dependent on each other for execution or independent.Example#include #include #include void* func(void* arg){    //detaching the current thread    pthread_detach(pthread_self());    printf("Inside the thread");    pthread_exit(NULL); } void fun(){    pthread_t ptid;    //creating a new thread    pthread_create(&ptid, NULL, &func, NULL);    printf("This line may be printed before thread terminates");    if(pthread_equal(ptid, pthread_self())       printf("Threads are equal");    else       printf("Threads are ... Read More

How to sum two integers without using arithmetic operators in C/C++ Program?

Ayush Gupta
Updated on 02-Mar-2020 11:02:56

461 Views

In this tutorial, we will be discussing a program to understand how to sum two integers without using arithmetic operators in C/C++.For adding two integers without using arithmetic operators, we can do this with either using pointers or using bitwise operators.ExampleUsing pointers#include using namespace std; int sum(int a, int b){    int *p = &a;    return (int)&p[b]; } int main() {    int add = sum(2,3);    cout

How to print a semicolon(;) without using semicolon in C/C++?

Ayush Gupta
Updated on 02-Mar-2020 10:38:18

246 Views

In this tutorial, we will be discussing a program to understand how to print a semicolon(;) without using a semicolon in /C++.This can be done in two possible ways, either by using the ascii value of semicolon or using user-defined macros for the same.Example Live DemoUsing putchar() method#include int main(){    //ASCII value of semicolon is equal to 59    if (putchar(59)){    }    return 0; }Output;Example Live DemoUsing Macros :#include #define POINT printf("%c",59) int main(){    if (POINT) {    } }Output;

Advertisements