You can use strlen() function from string.h header to get the length of string data.
#include <stdio.h>
#include <string.h>
//w ww . jav a2 s. c o m
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));
}
The code above generates the following result.
You may get a character by position index. string_var[index] syntax provides this feature.
Note 0 is the first index.
#include <stdio.h>
#include <string.h>
/*from www .j a va2 s.c o m*/
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]);
}
}
The code above generates the following result.
We can concatenate strings into one string.
We can use strcat() function from string.h header.
#include <stdio.h>
#include <string.h>
/* w w w .j av a 2 s.c om*/
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);
}
The code above generates the following result.
String type into numeric, you can use sscanf() function for String to numeric.
#include <stdio.h>
#include <string.h>
/*from w ww. j av a 2s. c o m*/
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);
}
The code above generates the following result.
To convert numeric to String type, use sprintf() function.
You can get String type automatically.
#include <stdio.h>
#include <string.h>
//from w w w .ja va2 s. c o m
void numeric_to_string();
int main(int argc, const char* argv[]) {
numeric_to_string();
return 0;
}
void numeric_to_string(){
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);
}
The code above generates the following result.
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.
#include <stdio.h>
#include <string.h>
/* ww w . ja v a 2 s . c o m*/
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);
}
}
The code above generates the following result.
You may copy some characters from String data.
use strcpy() and strncpy() functions.
#include <stdio.h>
#include <string.h>
// ww w . j a v a2 s . c o m
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);
}
The code above generates the following result.
C standard library stdlib.h provides a few functions that serve the purpose of converting strings to numbers.
atof-Converts a string to a floating-point number.
atoi-Converts a string to an integer.
Both of these functions are demonstrated in the next program.
#include <stdio.h>
#include <stdlib.h>
main() /*from www .j ava 2s. c o m*/
{
char *str1 = "123.79";
char *str2 = "55";
float x;
int y;
printf("\nString 1 is \"%s\"\n", str1);
printf("String 2 is \"%s\"\n", str2);
x = atof(str1);
y = atoi(str2);
printf("\nString 1 converted to a float is %.2f\n", x);
printf("String 2 converted to an integer is %d\n", y);
} //end main
The code above generates the following result.
The strcmp() function compares two strings for equality.
#include <stdio.h>
#include <string.h>
main() /* ww w . ja v a 2s . co m*/
{
char *str1 = "A";
char *str2 = "A";
char *str3 = "!";
printf("\nstr1 = %s\n", str1);
printf("\nstr2 = %s\n", str2);
printf("\nstr3 = %s\n", str3);
printf("\nstrcmp(str1, str2) = %d\n", strcmp(str1, str2));
printf("\nstrcmp(str1, str3) = %d\n", strcmp(str1, str3));
printf("\nstrcmp(str3, str1) = %d\n", strcmp(str3, str1));
if ( strcmp(str1, str2) == 0 )
printf("\nLetter A is equal to letter A\n");
if ( strcmp(str1, str3) > 0 )
printf("Letter A is greater than character !\n");
if ( strcmp(str3, str1) < 0 )
printf("Character ! is less than letter A\n");
} // end main
The strcmp() function takes two strings as arguments and compares them using corresponding character codes.
After comparing the two strings, the strcmp() function returns a single numeric value that indicates whether the first string is equal to, less than, or greater than the second string.
The following table describes the strcmp() function's return values in further detail.
Sample Function | Return Value | Description |
---|---|---|
strcmp(string1, string2) | 0 | string1 is equal to string2 |
strcmp(string1, string2) | <0 | string1 is less than string2 |
strcmp(string1, string2) | >0 | string1 is greater than string2 |
The code above generates the following result.