C -- string exercises, string exercises
1.
Searches for all characters that appear in string 1,
For example:
Input
Asdfwd
D
The output is
3
6
Input
Hhff
H
The output is
1
2
1 #include <string.h> 2 int main(int args,const char *argv[]) 3 { 4 char a[100] = "sgfgeasdefw"; 5 char target ; 6 scanf("%c",&target); 7 char *p = a; 8 while((p=strchr(p, target))) 9 {10 printf("%ld\n",p-a+1);11 p++;12 }13 }
2.
Compares whether a string 2 exists in string 1. If Y exists, N is output.
For example:
Input
Asdfwd
Dfw
The output is
Y
Input
Sgfgeasdfw
Hhff
The output is
N
1 # include <string. h> 2 int main (int args, const char * argv []) 3 {4 char a [100] = "sgfgeasdfw"; 5 const char * B = "ge "; 6 // The second parameter passed in. Although it is a string, it automatically parses each character corresponding to the bit, then judge the first position in string a 7 // strtok (a, B); 8 // strstr (const char *, const char *); returns the first position in the substring 9 if (strstr (a, B) 10 {11 printf ("Y "); 12} else {13 printf ("N"); 14} 15}
3.
Queries the number of times a string 2 appears in string 1,
For example:
Input
Asdfwd
D
The output is
2
Input
Hhff
H
The output is
2
1 # include <string. h> 2 int main (int args, const char * argv []) 3 {4 char * src = "asfdasdfassdf"; 5 char target [100] = {}; 6 scanf ("% s", target); 7 int cnt = 0; 8 char * p = src; 9 while (p = strstr (p, target ))) 10 {11 cnt ++; 12 p ++; 13} 14 printf ("number of occurrences: % d", cnt); 15 return 0; 16}
4.
Specify an arbitrary string and output it in reverse order.
For example:
Input
Asdfwd
The output is
Dwfdsa
1 # include <string. h> 2 char * reverse (char * a) 3 {4 int len = (int) strlen (a); 5 char B [100] = {0 }; 6 for (int I = len-1; I> = 0; I --) 7 {8 B [len-i-1] = a [I]; 9} 10 char * res = B; 11 return res; 12} 13 int main (int args, const char * argv []) 14 {15 char * a = "afdasfas "; 16 char * res = reverse (a); 17 printf ("% s", res); 18 return 0; 19}View Code
5.
Description: "eeeeeaaaff" is compressed to "e5a3f2". Please implement it by programming.
This question is embarrassing: If the string is "abc", if it is converted into "a1b1c1", it exceeds the length of the original string, and the question is given to the original position, we assume that the original length of the string is the length given by the question, so there is no way, so if the length of a single letter is not followed by 1
1 # include <stdio. h> 2 # include <string. h> 3 # include <stdlib. h> 4 5 void condense (char * src_str) 6 {7 if (src_str = NULL) 8 return; 9 10 int count = 1; 11 int sub_length = 1; 12 for (int I = 1; I <strlen (src_str); I ++) 13 {14 if (src_str [I] = src_str [I-1]) 15 {16 count ++; 17} 18 else19 {20 if (count = 1) 21 {22 src_str [sub_length ++] = src_str [I]; // assign a single value 23} 24 else25 {26 src_str [sub_length ++] = count + 48; // Add multiple numbers, + 48 to ascii code 27 src_str [sub_length ++] = src_str [I]; 28 count = 1; 29} 30} 31} 32 33 if (sub_length <strlen (src_str )) 34 src_str [sub_length] = '\ 0'; 35 36 printf ("% s \ n", src_str); 37 38 39} 40 41 int main () 42 {43 char str [] = "abbbdffeeg"; 44 condense (str); 45 getchar (); 46 return 0; 47}View Code
6. Description: "eeeeeaaaff" is compressed to "e5a3f2" and stored in a string. It is implemented through sub-functions. The results are returned using pointer functions. Please implement programming.
1 # include <string. h> 2 # include <ctype. h> 3 char * getStringToCompress (char * src) 4 {5 char compress [100] ={}; 6 int cnt = 0; 7 char * psrc = src; 8 char al [100] ={}; 9 int num [100] = {0}; 10 int I = 0; 11 while (* psrc) 12 {13 cnt ++; 14 if (* psrc! = * (Psrc + 1) 15 {16 num [I] = cnt; 17 al [I] = * psrc; 18 cnt = 0; 19 I ++; 20} 21 psrc ++; 22} 23 int len = (int) strlen (al); 24 char buf [100] = ""; 25 // convert the elements in the al and num arrays into character arrays, and keep them in the compress character array for 26 for (int h = 0; h <len; h ++) 27 {28 if (isalpha (al [h]) {29 sprintf (buf, "% c", al [h]); // use sprintf (char * buf, const char * format, argument ,...) returns 30 strcat (compress, buf); 31} 32 if (! Isalpha (num [h]) {33 sprintf (buf, "% d", num [h]); // use sprintf () the function converts a number to 34 strcat (compress, buf); 35} 36} 37 char * res = compress; 38 return res; 39} 40 int main (int args, const char * argv []) 41 {42 char src [100] = "eeeeeaaaaaaaaaff"; 43 char * compress = getStringToCompress (src); 44 printf ("% s ", compress); 45 return 0; 46}View Code