String Operations - Learning C by Example (2015)

Learning C by Example (2015)

6. String Operations

This chapter explains how to work with String operations in C.

6.1 Concatenating Strings

If you have a list of string, you can concatenate into one string. You can use strcat() function from string.h header. For illustration, create a file, called stringdemo.c, and write the following code.

#include <stdio.h>

#include <string.h>

void concatenating();

int main(int argc, const char* argv[]) {

concatenating();

return 0;

}

void concatenating(){

printf("====concatenating===\n");

char str1[30] = "hello";

char str2[10] = "wolrd";

strcat(str1,str2);

printf("result: %s\n",str1);

}

Compile and run this program.

$ gcc -o stringdemo stringdemo.c

$ ./stringdemo

A program output can be seen in Figure below.

ch6-1

6.2 String To Numeric

Sometime you want to do math operations but input data has String type. To convert String type into numeric, you can use sscanf() function for String to numeric. Add string_to_numeric() function on stringdemo.c and write this code.

#include <stdio.h>

#include <string.h>

void string_to_numeric();

int main(int argc, const char* argv[]) {

string_to_numeric();

return 0;

}

void string_to_numeric(){

printf("====string_to_numeric===\n");

char str1[10] = "10";

char str2[10] = "28.74";

int num1;

float num2;

sscanf(str1,"%d",&num1);

sscanf(str2,"%f",&num2);

printf("num1: %d\n",num1);

printf("num2: %f\n",num2);

}

Compile and run this program. If success, you get the following of program output.

ch6-2

6.3 Numeric to String

It is easy to convert numeric to String type, you can use sprintf() function. You can get String type automatically. For illustration, add numeric_to_string() function and write this code.

#include <stdio.h>

#include <string.h>

void numeric_to_string();

int main(int argc, const char* argv[]) {

numeric_to_string();

return 0;

}

void numeric_to_string(){

printf("====numeric_to_string===\n");

int n = 10;

float m = 23.78;

char num1[10];

char num2[10];

sprintf(num1,"%d",n);

sprintf(num2,"%.2f",m);

printf("num1: %s\n",num1);

printf("num2: %s\n",num2);

}

Compile and run this program.

ch6-3

6.4 String Parser

The simple solution to parsing String uses strtok() function with delimiter parameter. For example, you have String data with ; delimiter and want to parse it. Here is sample code by adding string_parser() function.

#include <stdio.h>

#include <string.h>

void string_parser();

int main(int argc, const char* argv[]) {

string_parser();

return 0;

}

void string_parser(){

char cities[40] = "Tokyo;Berlin;London;New York";

char token[2]=";";

char* city;

printf("cities: %s\n",cities);

city = strtok(cities, token);

while(city != NULL){

printf("%s\n", city );

city = strtok(NULL, token);

}

}

Compile and run this program.

ch6-4

6.5 Check String Data Length

You can use strlen() function from string.h header to get the length of string data. For testing, add string_length() function and write this code.

#include <stdio.h>

#include <string.h>

void string_length();

int main(int argc, const char* argv[]) {

string_length();

return 0;

}

void string_length(){

char str[20] = "Hello world";

printf("str: %s\n",str);

printf("length: %d\n",(int)strlen(str));

}

Compile and run this program.

ch6-5

6.6 Copy Data

You may copy some characters from String data. To do it, you can use strcpy() and strncpy() functions. For illustration, add string_copy() function and write the following code.

#include <stdio.h>

#include <string.h>

void string_copy();

int main(int argc, const char* argv[]) {

string_copy();

return 0;

}

void string_copy(){

char str[15] = "Hello world";

char new_str[20];

strcpy(new_str,str);

printf("str: %s\n",str);

printf("new_str: %s\n",new_str);

memset(new_str, '\0', sizeof(new_str));

strncpy(new_str,str,5);

printf("strncpy-new_str: %s\n",new_str);

}

Compile and run this program.

ch6-6

6.7 Exploring Characters

You may get a character by position index. string_var[index] syntax provides this feature. Note 0 is the first index.

For testing, add string_explore() function and write this code.

#include <stdio.h>

#include <string.h>

void string_explore();

int main(int argc, const char* argv[]) {

string_explore();

return 0;

}

void string_explore(){

char str[15] = "Hello world";

int index;

for(index=0;index<strlen(str);index++){

printf("%c\n",str[index]);

}

}

Compile and run this program.

ch6-7