如何在C中打印字符串数组的特定部分?
我有一个字符数组,由空格分隔的单词组成。 从输入读取数组。我想打印以元音开头的单词 我的代码:如何在C中打印字符串数组的特定部分?,c,arrays,string,insert,C,Arrays,String,Insert,我有一个字符数组,由空格分隔的单词组成。 从输入读取数组。我想打印以元音开头的单词 我的代码: #include <stdio.h> #include <string.h> #include <stdlib.h> char *insertt (char dest[100], const char sou[10], int poz) { int cnt=0; char *fine = (char *) malloc(sizeof(char)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *insertt (char dest[100], const char sou[10], int poz) {
int cnt=0;
char *fine = (char *) malloc(sizeof(char) * 3);
char *aux = (char *) malloc(sizeof(char) * 3);
strncpy(fine,dest,poz);
strcat(fine,sou);
do {
aux[cnt]=dest[poz];
poz++;
cnt++;
}
while (poz<=strlen(dest));
strcat(fine,aux);
free(aux);
return fine;
}
int check_vowel(char s) {
switch (s) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
return 1;
default: return 0;
}
}
int main (void) {
const char spc[]=" ";
char s[100];
char *finale = (char *) malloc(sizeof(char) * 3);
int beg,len,aux; //beg - beginning; len - length;
int i = 0;
printf("s=");
gets(s);
finale = insertt(s,spc,0); //the array will start with a space
do {
if (finale[i]==' ' && check_vowel(finale[i+1])==1) {
beg=i+1; //set start point
do { //calculate length of the word that starts with a vowel
len++;
}
while(finale[len]!=' '); //stop if a space is found
printf("%.*s\n", len, finale + beg); //print the word
}
i++;
}
while (i<=strlen(finale)); //stop if we reached the end of the string
free(finale);
return 0;
}
我肯定问题出在主do-while循环的某个地方,但我就是不知道可能是什么。。。
这两个功能工作正常。
谢谢 您的程序中有很多错误,可能会导致
SIGSEGV
1) 函数insertt
中的fine
和aux
指针未分配足够的内存。改为:
char *fine = malloc(sizeof(char) * 300);
2) 在外部do while{}
循环中,条件必须为:
i<strlen(finale)
while(len<strlen(finale) && finale[len]!=' ');
编辑:
您的代码中仍然存在一个逻辑错误
,我将其留给您来解决:
对于字符串:
adfajf isafdadsf ifsfd
您的输出是:
adfajf
isafdadsf ifsfd
ifsfd
这是不正确的 另一种方法,使用小型有限状态机。字符串已在适当位置修改。战略性地使用
break
和continue
可以避免很多条件。
(在第二个和下一个选定单词之前添加空格的唯一剩余条件)
#包括
#包括
int分类(int ch)
{
开关(ch){
案例“a”:案例“a”:
案例“e”:案例“e”:
案例“i”:案例“i”:
案例“o”:案例“o”:
案例“u”:案例“u”:
案例“y”:案例“y”:
返回1;
案例“”:案例“\t”:案例“\n”:
返回0;
违约:
返回2;
}
}
仅限大小(字符*str)
{
尺寸src,dst;
int型,state;
for(state=0,src=dst=0;str[src];src++){
类型=分类(str[src]);
开关(状态){
案例0:/*首字母*/
if(type==1){state=1;if(dst>0)str[dst++]='';break;}
如果(type==2){state=2;}
继续;
例1:/*在元音词中*/
如果(type==0){state=0;continue;}
打破
案例2:/*在抑制字中*/
如果(type==0){state=0;}
继续;
}
str[dst++]=str[src];
}
str[dst]=0;
返回dst;
}
内部主(空)
{
尺寸透镜;
字符字符串[]=“abc def gh ijk lmn opq rst uvw xyz”;
printf(“字符串:='%s'\n',字符串);
len=仅限Klingers_(字符串);
printf(“Return=%zu,String:='%s'\n',len,String”);
返回0;
}
正如我所说,这两个函数(insertt和check元音)工作正常,它们完全执行了它们应该执行的操作:)将输入读入字符串数组难道不更容易吗?对我来说很有用:jrenner@pc:~$./test s=今天奥利病了奥利病了你的fine
和aux
变量在insertt()中
只有3个字符长有限状态机只需要三个状态和三种令牌类型。请不要建议强制转换malloc()
的返回值,即使OP的代码包含该错误。嘿!非常感谢。在应用您的建议后,它不再进行核心转储。我还注意到可能有一个逻辑错误,但我不知道在哪里。。。无论如何,对于“adfajf isafdadsf ifsfd”,我的输出是:adfajf isafdadsf ifsfd\n isafdadsf ifsfd\n ifsfd\n
这很奇怪,但我会在代码中搜索逻辑错误:)
adfajf
isafdadsf ifsfd
ifsfd
#include <stdio.h>
#include <string.h>
int classify(int ch)
{
switch(ch) {
case 'a': case 'A':
case 'e': case 'E':
case 'i': case 'I':
case 'o': case 'O':
case 'u': case 'U':
case 'y':case 'Y':
return 1;
case ' ': case '\t': case '\n':
return 0;
default:
return 2;
}
}
size_t klinkers_only(char *str)
{
size_t src,dst;
int type,state;
for (state=0, src=dst=0; str[src] ; src++) {
type = classify( str[src] );
switch(state) {
case 0: /* initial */
if (type == 1) { state =1; if (dst>0) str[dst++] = ' '; break; }
if (type == 2) { state =2; }
continue;
case 1: /* in vowel word */
if (type == 0) { state=0; continue; }
break;
case 2: /* in suppressed word */
if (type == 0) { state=0; }
continue;
}
str[dst++] = str[src];
}
str[dst] = 0;
return dst;
}
int main(void)
{
size_t len;
char string[] = "abc def gh ijk lmn opq rst uvw xyz";
printf("String:='%s'\n", string );
len = klinkers_only(string);
printf("Return= %zu, String:='%s'\n", len, string );
return 0;
}