我的字符串生成程序有什么问题?

时间:2014-09-10 16:52:39

标签: c string memory concurrency buffer

我有一个带7个参数的程序。现在,第一个参数被忽略了。我的主函数fcfsa有8个参数:s1,s2,x1,y1,z1,x2,y2,z2。 s1和s2是char指针变量,x1..z2是argv中连续顺序的最后6个整数参数。

fcfsa应该这样做: 第一个字符串s1将由x1 R组成,后跟y1 w&s,然后是z1 R' s。 第二个字符串s2将由x1 r组成,接着是x2 R,然后是y2 w&s,接着是z2 R' s。

但是在使用./main 0 4 2 7 3 6 5执行程序时,我没有得到正确的输出 现在再次忽略第一个参数0。

这是我的输出:

inputs: 0 4 2 7 3 6 5 
maxSize=27

Part 1

RRRRwwRRRRRRRRRRRR+Y?
rrrrRRRwwwwww

0 4 2.0 0.86364

我的main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pslibrary.h"

void part0(char *s1, char *s2);
void display(char *heading, char *s1, char *s2);
void fcfsa(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2);

int main(int argc, char **argv) {
    int i;
    printf("Assignment 0 program was written by Marcus Lorenzana\n");
    if (argc != 8) {
        printf("Error. Wrong number of arguments\n");
        return 1;
    }
    printf("inputs: ");
    for (i = 1; i < 8; i++) {
        printf("%s ",argv[i]);
    }
    printf("\n");

    //Get maximum string size
    int maxSize=0;
    for (i = 1; i < 8; i++) {
        maxSize+=atoi(argv[i]);
    }
    printf("maxSize=%d\n",maxSize);

    char str1[maxSize],str2[maxSize];

    fcfsa(str1,str2,atoi(argv[2]),atoi(argv[3]),atoi(argv[4]),atoi(argv[5]),atoi(argv[6]),atoi(argv[7]));
   display("Part 1\n",str1,str2);

        return 0;
}

我的程序包含fcfsa:

#include <stdio.h>
#include <string.h>
#include "pslibrary.h"

void part0(char *s1, char *s2){
    strcpy(s1,"RRwwwwwRRRRRRRRR"); 
    strcpy(s2,"rrRRRRwwwwwwwwrrRRRRRRR"); 
}

void display(char *heading, char *s1, char *s2){
    printf("\n"); 
    printf("%s\n",heading); 
    printf("%s\n",s1); 
    printf("%s\n",s2); 
    printf("\n"); 
    int s1len = strlen(s1); 
    int s2len = strlen(s2); 
    int i,s1cnt,s2cnt,s1cnt2,s2cnt2;
    s1cnt=s2cnt=0;  
    s1cnt2=s2cnt2=0;
    for (i = 0; i < s1len; i++) {
        if (s1[i]=='r')
            s1cnt++; 
    }
    for (i = 0; i < s2len; i++) {
        if (s2[i]=='r')
            s2cnt++; 
    }
    float average_r = (s1cnt+s2cnt)/2; 

    for (i = 0; i < s1len; i++) {
        if (s1[i]=='R')
            s1cnt2++; 
    }
    for (i = 0; i < s2len; i++) {
        if (s2[i]=='R')
            s2cnt2++; 
    }

    int longest; 
    if (s2len > s1len) {
        longest = s2len; 
    } else {
        longest = s1len; 
    }

    float average_R = (float)(s1cnt2+s2cnt2)/longest;

    printf("%d %d %.1f %.5f\n",s1cnt,s2cnt,average_r,average_R); 
}

void fcfsa(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2){
    //s1: x1 R's, y1 w's, 0 or more r's, z1 R's
    //s2: x1 r's, x2 R's, y2 w's, 0 or more r's, z2 R's 
    int i;
        //s1 fill
    int s1_start=0;
        int s1_end=x1;  
    for (i = s1_start; i < s1_end; i++) {
        s1[i]='R';
    } 
    s1_start=s1_end; 
    s1_end+=y1; 
    for (i = s1_start; i < s1_end; i++) {
        s1[i]='w';
    }
    s1_start=s1_end;
    s1_end+=z1;
    for (i = s1_start; i < s1_end; i++){
        s1[i]='R'; 
    }
    s1[s1_end]='\0';
        //printf("s1:%s\n",s1);     
    //s2 fill
    int s2_start=0;
    int s2_end=x1;
    for (i = s2_start; i < s2_end; i++) {
        s2[i]='r';
    }
    s2_start=s2_end; 
    s2_end+=x2;
    for (i = s2_start; i < s2_end; i++) {
        s2[i]='R';
    }
    s2_start=s2_end;
    s2_end+=y2;
    for (i = s2_start; i < s2_end; i++) {
        s2[i]='w';
    }
    s2_start=s2_end;
    s2_end+=z2; 
    for (i = s2_start; i < s2_end; i++) {
        s1[i]='R';
    }
    s2[s2_end]='\0';
    //printf("s2:%s\n",s2); 
}

3 个答案:

答案 0 :(得分:0)

您偶尔会遇到一些SegFault问题吗?我认为你应该尝试malloc'ing两个新的字符串,x1 + y1 + z1作为第一个的大小,x2 + y2 + z2作为填充前的第二个的大小。

char *str1 = (char *)malloc(sizeof(char) * (x1 + y1 + z1 + 1)); // +1 for the '\0'
// here goes the code to fill str1
printf("str1:%s\n", str1);
free(str1);

不要忘记#include <stdlib.h>

答案 1 :(得分:0)

代码中的错误:

s2_start=s2_end;
s2_end+=z2; 
for (i = s2_start; i < s2_end; i++) {
    s1[i]='R';
 // ^^^^^^^  error
}
s2[s2_end]='\0';

那一行应该是:

    s2[i]='R';

您可以改变您的编码习惯,以避免犯这样的错误。例如,将fcfsa分成两个函数。

void fcfsa(char *s1, char *s2, int x1, int y1, int z1, int x2, int y2, int z2){
   fcfsa1(s1, x1, y1, z1);
   fcfsa2(s2, x1, x2, y2, z2);
}

,其中

void fcfsa1(char *s, int x1, int y1, int z1){
   //s: x1 R's, y1 w's, 0 or more r's, z1 R's
   int i;
   int start=0;
   int end=x1;  
   for (i = start; i < end; i++) {
      s[i]='R';
   } 

   start=end; 
   end+=y1; 
   for (i = start; i < end; i++) {
      s[i]='w';
   }
   start=end;
   end+=z1;
   for (i = start; i < end; i++){
      s[i]='R'; 
   }
   s[end]='\0';
}

void fcfsa2(char *s, int x1, int x2, int y2, int z2){
    //s: x1 r's, x2 R's, y2 w's, 0 or more r's, z2 R's 
    int i;
    int start=0;
    int end=x1;
    for (i = start; i < end; i++) {
        s[i]='r';
    }
    start=end; 
    end+=x2;
    for (i = start; i < end; i++) {
        s[i]='R';
    }
    start=end;
    end+=y2;
    for (i = start; i < end; i++) {
        s[i]='w';
    }
    start=end;
    end+=z2; 
    for (i = start; i < end; i++) {
        s[i]='R';
    }
    s[end]='\0';
}

您可以通过编写辅助函数来使用字符填充数组来进一步简化代码。

void fill(char *s, int start, int end, char c)
{
   int i;
   for (i = start; i < end; i++) {
      s[i]=c;
   } 
}

然后,fcfsa1fcfsa2可以简化为:

void fcfsa1(char *s, int x1, int y1, int z1){
   //s: x1 R's, y1 w's, 0 or more r's, z1 R's
   int start=0;
   int end=x1;  
   fill(s, start, end, 'R');

   start=end; 
   end+=y1; 
   fill(s, start, end, 'w');

   start=end;
   end+=z1;
   fill(s, start, end, 'R');

   s[end]='\0';
}

void fcfsa2(char *s, int x1, int x2, int y2, int z2){
    //s: x1 r's, x2 R's, y2 w's, 0 or more r's, z2 R's 
    int start=0;
    int end=x1;
    fill(s, start, end, 'r');

    start=end; 
    end+=x2;
    fill(s, start, end, 'R');

    start=end;
    end+=y2;
    fill(s, start, end, 'w');

    start=end;
    end+=z2; 
    fill(s, start, end, 'R');

    s[end]='\0';
}

答案 2 :(得分:0)

void change(char* s,int position,char nChar);
void fcfsa(char *s1, char *s2,int x1,int y1,int z1,int x2,int y2,int z2){
    int i,position=0;
    //*s1=(char*)calloc(x1+y1+z1+1,sizeof(char));
    //*s2=(char*)calloc(x1+x2+y2+z2+1,sizeof(char));

    //initialization of s1
    for(i=0;i<x1;i++){
          s1[position]='R';
          position++;
    }
    for(i=0;i<y1;i++){
          s1[position]='w';
          position++;
    }
    for(i=0;i<z1;i++){
          s1[position]='R';
          position++;
    }
    s1[position]='\0';


    //initialization of s2
    position=0;
    for(i=0;i<x1;i++){
          s2[position]='r';
          position++;
    }
    for(i=0;i<x2;i++){
          s2[position]='R';
          position++;
    }
    for(i=0;i<y2;i++){
          s2[position]='w';
          position++;
    }
    for(i=0;i<z2;i++)
    {
          s2[position]='R';
          position++;
    }
    s2[position]='\0';

    //printf("\ns1=%s",s1);
    //printf("\ns2=%s",s2);

    i=0;
    while(s1[i]!='\0' && s2[i]!='\0')
    {
            if(s1[i]=='R' && s2[i]=='R')
            {
                    if(s2[i]==s2[i-1])
                    {
                         change(s1,i,'r');

                    }
                    else
                    {
                     change(s2,i,'r');
                    }
            }
            i++;
    }

}

void change(char* s,int position,char nChar){

int i,length;
//char *ptr;
length=strlen(s);
//ptr=(char*)calloc(length+2,sizeof(char));
//strcpy(ptr,s);

for(i=length;i>=position;i--)
{
         s[i+1]=s[i];
}
s[position]=nChar;

}