简体   繁体   中英

C - adding to an Char Array and Int

Basically, I have an array being passed as a pointer to a function, which capitalizes the first letter, the strlen of that array is also being passed.

What I want to do, after capitalization, is to then append '1' to the end of the array, so that hello becomes Hello1 . But I am unsure how to do such thing in code, or whether its possible.

...
char myString[70];  <-- Array being passed, where the string is never > 64 char's.
...

void capFirst(char *s, int i) {
    s[0] = (toupper(s[0]));
}

EDIT: Solutions below do the following:

1Security
1Sebastian
1Schumacher
1Sanderson

EDIT:

void capFirst(char *s, int i) {
 s[0] = toupper(s[0]);
 s[i] = 1;
 s[i+1] = '\0';
}


int main(int argc, char** argv) {
int i;
int j = 0;
char fileSave[256];
char myString[50];
FILE *fpIn, *fpOut;
for(i = 1; i < argc; i++) {
    fpIn = fopen(argv[i], "rb");
    snprintf(fileSave, 256, "%s~[%d]", argv[i], i);
    fpOut= fopen(fileSave, "wb");
    while((fgets(myString, 49, fpIn)) != NULL) {
        if(isspace(myString[0]))
            break;
        j = strlen(myString);
        if( (j > 8) && (j < 64) ) {
            capFirst(myString, strlen(myString));
            fprintf(fpOut, "%s", myString);
        }
    }

}
return 0;
}

UPDATE:

void capFirstappOne(char *s, int i) {
    s[0] = toupper(s[0]);
    s[i] = '1';
    s[i+1] = '\0';
}


int main(int argc, char** argv) {
int i; int j = 0;
char fileSave[256];
char myString[50];
FILE *fpIn, *fpOut;
for(i = 1; i < argc; i++) {
    fpIn = fopen(argv[i], "rb");
    snprintf(fileSave, 256, "%s~[%d]", argv[i], i);
    fpOut= fopen(fileSave, "wb");
    while((fgets(myString, 64, fpIn)) != NULL) {
        j = strlen(myString);
        if (j > 0 && (myString[j-1] == '\n')) {
            myString[j-1] = '\0';
        }
        if( j > 8) {
            capFirstappOne(myString, strlen(myString));
            fprintf(fpOut, "%s\n", myString);
        }
    }

}
return 0;
}

Try this (if the array is large enough):

s[i] = '1';
s[i + 1] = 0;

You must be sure that the array have enough space to take an extra 1 towards the end. And do not miss to terminate the string with '\\0' .

void capFirst(char *s, int i) {
    s[0] = toupper(s[0]);
    s[i] = '1';
    s[i+1] = '\0';
}

Or use strcat() function.

void capFirst(char *s, int i) {
    s[0] = toupper(s[0]);
    strcat(s, "1");
}

The full program is as follows:

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

void capFirst1(char *s, int i)  
{
    s[0] = toupper(s[0]);
    strcat(s, "1");
}

void capFirst2(char *s, int i)  
{
    s[0] = toupper(s[0]);
    s[i] = '1';
    s[i+1] = '\0';
}

int main(void)
{
    char a[10] = {"hello"};
    char b[10] = {"world"};

    capFirst1(a, strlen(a));
    printf("a = %s\n", a); 

    capFirst2(b, strlen(b));
    printf("b = %s\n", b); 

    return 0;
}

The function appears to put the '1' at the front of the string because the string originally ends with a newline. So when appending a '1' to that, the result looks like "Sebastian\\n1". When these strings are printed to the output file - without any separators - the result looks like

Sebastian
1Schumacher
1Sanderson
1

When reading a file with fgets , check whether a newline has been read, and if the newline is not supposed to be part of the string in further processing, remove it (overwrite it with '\\0'). When writing to a file with fprintf , it prints out only the characters it is passed, it doesn't add any separators, be it spaces or newlines, so one has to add the desired separators oneself.

In your code,

while((fgets(myString, 49, fpIn)) != NULL) {
    if(isspace(myString[0]))
        break;
    j = strlen(myString);
    if( (j > 8) && (j < 64) ) {
        capFirst(myString, strlen(myString));
        fprintf(fpOut, "%s", myString);
    }
}

to remove unwanted newlines, you could, after j = strlen(myString); , insert

    if (j > 0 && myString[j-1] == '\n') {
        myString[--j] = 0;
    }

Then a newline would be overwritten ( fgets doesn't read more than one newline) and the variable holding the length decremented accordingly. You needn't recompute strlen then, you can call capFirst with

    if (j > 8) {
        capFirst(myString,j);
        fprintf(fpOut, "%s\n", myString);
    }

In the condition, you need not check j < 64 because you told fgets to not read more than 48 characters (49-1, one for the 0-terminator), so that is a given. In the snippet above, I've added a newline to the format string, so each string is printed on its own line. If you don't want that, you could use "%s " to separate the strings with spaces, "%s," for commas etc.

Here is how you can do it

char myString[70];
.....
void capFirst(char *s, int i) {
    s[0] = (toupper(s[0]));
    s[i] = '1';
    s[i+1] = 0;//NULL Termination
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM