输入一个字符串str1,把其中的连续非数字的字符子串换成一个‘*’,存入字符数组str2 中。
所有数字字符也必须依次存入 str2 中。输出str2其中可能包含空格。字符串长度不超过80个字符。
#include<bits/stdc++.h>
using namespace std;
int cnt;
string s1, s2;
bool flag;
int main(){
getline(cin, s1);
for(int i = 0; i < s1.size(); i++){
if(s1[i] < '0' || s1[i] > '9'){
s1[i] = '*';
}
}
for(int i = 0; i < s1.size(); i++){
if(s1[i] != '*'){
if(flag){
flag = 0;
s2[++cnt] = '*';
s2[++cnt] = s1[i];
}else{
s2[++cnt] = s1[i];
}
}else if(s1[i] == '*') flag = 1;
}
for(int i = 1; i <= cnt; i++) cout << s2[i];
return 0;
}
不知道你这个问题是否已经解决, 如果还没有解决的话:#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char *str1;
char *str2 = "Hello";
char *str3 = "World";
/*
1,strcpy(dest,src)
此函数原型为 char *strcpy(char* dest, const char *src)
功能为拷贝字符串内容到目的串,把src所指向的内容拷贝到dest
*/
str1 = (char *)calloc(1,sizeof(char));
strcpy(str1,str2);
printf("%s\n",str1); // Hello
/*
2,strcmp(str1,str2)
此函数的函数原型为 int strcmp(const char *str1, const char *str2).
功能为比较两个字符串。
当str1指向的字符串大于str2指向的字符串时,返回正数。
当str1指向的字符串等于str2指向的字符串时,返回0。
当str1指向的字符串小于str2指向的字符串时,返回负数。
*/
if(strcmp(str1,str2)==0){
printf("String1 equal to String2 !\n");
}
/*
3,strlen(str)
此函数原型为unsigned in strlen(const char *str)
功能为返回字符串str的长度(不包括'\0')。
*/
printf("%lu\n",strlen(str1)); //5
/*
4,strcat(dest,src)
此函数原型为 char *strcat(char *dest, const char *src).
功能为连接两个字符串,把src连接到dest后面;返回dest地址
*/
strcat(str1,str3);
printf("%s\n",str1); //HelloWorld
/*
5、strstr()
函数原型为char *strstr(const char str1, const char *str2)
功能为查找字符串str2在str1中是否出现,找到则返回str1从找到位置开始之后的字符串,否则返回NULL。
*/
char *res = strstr(str1,"llo");
if(!res) {
printf("s2 is not in s1!\n");
} else {
printf("s2 appear in s1 in pos: %s\n",res); //lloworld
}
/*
6,strchr() strrchr()
char *strchr(char *str, char c)
功能为返回str中首次出现c的位置之后的字符串,如果str中不含c返回NULL
char *strrchr(char *str, char c)
功能为返回str中最后一次出现c的位置之后的字符串,如果str中不含c返回NULL
*/
printf("%s\n",strchr(str1,'o')); //oWorld
printf("%s\n",strrchr(str1,'o')); //orld
/*
7、strcspn() strspn()
strcspn
原型:size_t strcspn(const char *pstr, const char *strCharset)
在字符串pstr中搜寻strCharsret所包含字符在pstr中第一次出现的位置
strspn
原型size_t strspn(const char *pstr, const char *strCharset)
功能:返回pstr字符串中第一个不在后者出现的下标。
*/
unsigned long n = strcspn(str1,"World");
printf("char in s2 first appear in s1 is %c; pos in s1 is %lu\n",str1[n],n);// l,2
printf("%lu\n",strspn(str1,"HelloKitty")); //5
/*
8、strtoul
原型为unsigned long strtoul(const char *nptr,char **endptr,int base);
将参数nptr字符串根据参数base来转换成无符号的长整型数,endstr 为第一个不能转换的字符的指针。
*/
char *arg = "1234a5";
char *endstr=NULL;
if(strtoul(arg, &endstr, 10) > 65535 || !endstr || '\0' != *endstr){
printf("wrong endstr: %s!\n", endstr); // a5
} else {
printf("%lu is uint16!\n", strtoul(arg, &endstr, 10));
}
/*
9、strdup()
此函数原型为char *strdup(const char *str)
功能为拷贝字符串到新建的内存,返回内存指针。若失败,返回NULL。要注意,返回的指针指向的内存在堆中,所以要手动释放。
*/
/*
10、strtok
函数原型为char *strtok(char s[], const char *delim);
功能为分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。
在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。
每次调用成功则将delim替换为‘\0’,返回指向被分割出片段的指针。
*/
char str[] = "Jack,Kimi,Lily,Lucy";
//对str_new进行strtok操作,防止str被更改
char *str_new = strdup(str);
//arr用来保存结果
char **arr =(char **)calloc(1,sizeof(char *));
int i = 0;
char *temp = strtok(str_new,",");
while(temp)
{
arr[i] = (char *)calloc(1, sizeof(char));
strcpy(arr[i], temp);
temp = strtok(NULL,",");
i++;
}
for(int j = 0; j < i; j++){
printf("arr[%d]:%s\n", j, arr[j]);
}
free(arr);
free(str_new);
return 0;
}