How and when the output of other characters, find fault?

Category: C/C++ -> C Author: quweixin200 Date: 2006-01-25 10:42:40
 
quweixin200
2006-01-25 10:42:40
/ / Put a string of vowels was copied into another string
# include <stdio.h>
void cz (char str_1 [], char str_2 [])
{
int i, j = 0;
for (i = 0; str_1 [i]! = '\ 0'; i + +)
{
if ((str_1 [i]> = 'a') && (str_1 [i] <= 'z'))
{
str_2 [j] = str_1 [i];
j + +;
}
}
puts (str_2);
return;
}
void main ()
{
char str_1 [80], str_2 [80];
printf ("Please enter a string \ n");
gets (str_1);
cz ( str_1, str_2);
return;
}
ysminson
2006-01-25 10:48:17
your program is copied vowels do?
SmallMosquito
2006-01-25 10:56:30
str_2 you did not first cleared ah
xiaomingxia
2006-01-25 11:11:54
Error reason: After not initialize the array definition, there will be unexpected character
Correction:
in char str_1 [80], str_2 [80]; adding this line

memset(str_1,0x00,sizeof(str_1));
memset(str_2,0x00,sizeof(str_2));

zjb4116064
2006-01-25 11:14:01
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

char ch1[200];

void init()
{
int i;
for(i=0;i<200;i++)
{
ch1[i]=0;
}

ch1['a']=1;
ch1['a'-32]=1;
ch1['e']=1;
ch1['e'-32]=1;
ch1['i']=1;
ch1['i'-32]=1;
ch1['o']=1;
ch1['o'-32]=1;
ch1['u']=1;
ch1['u'-32]=1;
}
void test(char src[],char dest[])
{
int i=0,j=0;

while(src[i]!='\0')
{
if(ch1[src[i]]==1)
{
dest[j++]=src[i];
}

i++;
}
}

int main()
{
char a[]="aeruiopnjmgcsawaaai";
char b[50]={0};

init();
test(a,b);

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

getchar();
return 0;
}
pangzi326
2006-01-25 11:25:46
void cz (char str_1 [], char str_2 []) function parameters do you put the phrase about amending: void cz (const char * str_1, char * str_2); This amendment errors with your results did not matter much, but doing better.
In this implementation function, I found your str_2 [] array did not put '\ 0' added, which is a sign of the end of the string. Why do not you at the end, that puts (str_2); preceded by a str_2 [j] = '\ 0'; Then try, no strings and you then initialize it. str_2 [80] = {0};
a940832505
2006-01-25 11:43:10
best memory applications manually initialize or clear about yourself in order to avoid unnecessary errors.
liuluodan
2006-01-25 12:00:38
nothing directly to the data stored in the array, there is no question what the initialization does not initialize Kazakhstan.
(PS: Do not put the other high-level language design ideas come inside to the C language, C language standard does not force programmers to initialize the variable, initialization will only reduce efficiency meaningless only)

Note that, copy the contents of the string will not put string terminator '\ 0' Copy the past (because the loop in the source string pointer '\ 0' at the end), so the need to manually after copying is finished target string to add terminator.

Again, LZ written procedures are not copied vowel, but copying lowercase letters Ha:

#include<stdio.h>

void cz(char str_1[],char str_2[])
{
int i,j=0;

for(i=0;str_1[i]!='\0';i++)
{
if((str_1[i]>='a') && (str_1[i]<='z'))
{
str_2[j]=str_1[i];
j++;
}
}

str_2[j]='\0'; //

puts(str_2);
return ;
}

void main()
{
char str_1[80],str_2[80];
printf(" \n");
gets(str_1);
cz(str_1,str_2);
return ;
}

if it is To copy the vowels, then logic should be like this:

#include<stdio.h>

void cz(char*,char*);
int isVowel(char);

int main()
{
char str_1[80],str_2[80];

printf(" \n");
gets(str_1);
cz(str_1,str_2);
puts(str_2);

return(0);
}

void cz(char *src,char *dest)
{
for(;*src;src++)
if(isVowel(*src))
*dest++=*src;

*dest=0;
}

int isVowel(char c)
{
return(c=='A'||c=='a'
||c=='E'||c=='e'
||c=='I'||c=='i'
||c=='O'||c=='o'
||c=='U'||c=='u');
}
zhziwe
2006-01-25 12:04:11
5 Floor positive solution, str2 [] at the end did not add '\ 0' character string does not end when the output will go to until there is '\ 0' position end output.