0

This is my code:

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

void splitString(char s[]) {

    char firstHalf[100] = { 0 };
    char secndHalf[100] = { 0 };

    for (int i = 0; i < strlen(s) / 2; i++){
        firstHalf[i] = s[i];
    }

    for (int i = strlen(s) /2; i < strlen(s); i++){
        secndHalf[i - strlen(s) / 2] = s[i];
    }

    printf("The string split in two is '%s, - %s' \n", firstHalf, secndHalf);
}

void upperCase(char s[]){

    //String in upper case
    for (size_t i = 0; i < strlen(s); i++)
    s[i] = toupper(s[i]);

        printf("The string in uppercase is '%s'", s);
}

void lowerCase(char s[]){

    //String in lower case
    for (size_t i = 0; i < strlen(s); i++)
        s[i] = tolower(s[i]);

        printf("The string in lowercase is '%s'", s);
}

int main() {

    char s[200];
    char splitS[200];

    printf("Type a string: ", sizeof( s));

    if (fgets(s, sizeof(s), stdin) != 0){
        printf("The string is '%s'", s);
    }

    strcpy(splitS, s);

    upperCase(s);
    lowerCase(s);
    splitString(splitS);

    return 0;
}

The correct way it's supposed to print is like this:

The string is 'Hello world'

The string in uppercase is 'HELLO WORLD'

The string in lowercase is 'hello world'

The string split in two is 'Hello, - world'

But instead it prints like this:

The string is 'Hello world

'The string in uppercase is 'HELLO WORLD

'The string in lowercase is 'hello world

'The string split in two is 'Hello , - world

'

Mohan
  • 1,871
  • 21
  • 34
Gaute
  • 3
  • 4
  • 2
    `printf("Type a string: ", sizeof( s));`.. WTH is this? – Sourav Ghosh Oct 19 '16 at 10:06
  • Consider flushing the `stdout`..... – Sourav Ghosh Oct 19 '16 at 10:08
  • 1
    either `fflush()`,or, `stdout` being line buffered, use `\n` at end of each format string. – Sourav Ghosh Oct 19 '16 at 10:08
  • Make sure the string you have input (that is, after `fgets`) does not contain a terminating newline character. That newline gets printed out by `printf`-s and mis-formats your results. :) – CiaPan Oct 19 '16 at 10:16
  • Oh, and add a newline (`\n`) at the end of each output formatting string; that will help you see what's going on. – CiaPan Oct 19 '16 at 10:17
  • Please read how to create a [mcve]. The `upperCase()`, `lowerCase()`, and `splitString()` are all irrelevant - your problem is adequately demonstrated with just the `gets()`+`printf()`, and more likely to attract good, targeted answers were you to do so. – Toby Speight Oct 19 '16 at 11:05

5 Answers5

1

You need to read the documentation for fgets() (my emphasis):

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

Since you are typing in these lines with line break characters at the end, you need to remove them in your code.

r3mainer
  • 23,981
  • 3
  • 51
  • 88
1

You have to put null terminator

void splitString(char s[]) {

char firstHalf[100] = { 0 };
char secndHalf[100] = { 0 };

// u need to add null terminator '\0' at the end of string
// so u can add it in for loop or set i outside of loop

for (size_t i = 0; i < strlen(s) / 2; i++){
    firstHalf[i] = s[i];
    **firstHalf[i+1] = '\0';**
}

for (size_t i = strlen(s) /2; i < strlen(s); i++){
    secndHalf[i - strlen(s) / 2] = s[i];
    **secndHalf[i+1] = '\0';**
}

printf("The string split in two is '%s, - %s' \n", firstHalf, secndHalf);

}

Sone
  • 11
  • 1
0

The fgets function will read a newline and append it to the input string of there's room for it.

You need to check if the last character in the string is \n and if so set it to zero to trim it.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

This is happening because fgets retains a newline at the end of the input string, and also because you do not printf a newline yourself.

So the result, is that newline is being printed in the wrong place, splitting your message, and the ' appears on the next line.

An easy way to remove the newline from the entry is with

s [ strcspn(s, "\r\n") ] = 0;

but don't forget to add the \n to the end of the printf formatting strings.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

I thing use scanf with format specifier %[^\n]s so you can skip the new line instated of fgets and add \n in every printf.

Complete working code :

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

void splitString(char s[]) {

    char firstHalf[100] = { 0 };
    char secndHalf[100] = { 0 };
int i;
    for ( i = 0; i < strlen(s) / 2; i++){
        firstHalf[i] = s[i];
    }

    for ( i = strlen(s) /2; i < strlen(s); i++){
        secndHalf[i - strlen(s) / 2] = s[i];
    }

    printf("\n The string split in two is '%s, - %s' \n", firstHalf, secndHalf);
}

void upperCase(char s[]){

    //String in upper case
    int i;
    for (i = 0; i < strlen(s); i++)
    s[i] = toupper(s[i]);

        printf("\n The string in uppercase is '%s'", s);
}

void lowerCase(char s[]){

    //String in lower case
    int i;
    for ( i = 0; i < strlen(s); i++)
        s[i] = tolower(s[i]);

        printf("\n The string in lowercase is '%s'", s);
}

int main() {

    char s[200];
    char splitS[200];

    printf("Type a string: %ld", sizeof( s));

    if (scanf("%200[^\n]s", s)!= 0){  //fgets(s, sizeof(s), stdin) 
        printf("\n The string is '%s'", s);
    }

    strcpy(splitS, s);

    upperCase(s);
    lowerCase(s);
    splitString(splitS);

    return 0;
}

OR

if you want use fgets only then find new line char in string and make it NULL and add new line char '\n' in every printf .

Code need to change is:

 if ( fgets(s, sizeof(s), stdin) != 0){   
     int  i;
     for(i = 0 ; i < sizeof( s) ; i++ ){
          if(s[i] == '\n'){
               s[i] = '\0';     
               break;
          }
      }
    printf("\n The string is '%s'", s); 
 }
Mohan
  • 1,871
  • 21
  • 34
  • It's just that the teacher wanted us to use fgets, as having a limit to a string written by a user could be dangerous. Not sure if I completely understood how it could be dangerous, but think it has something to do with this: http://stackoverflow.com/questions/2430303/disadvantages-of-scanf – Gaute Oct 19 '16 at 10:46
  • @Zuflus correct but to avoid buffer overflow i have given limit in the `scanf` that is `%200....` so it will be ok to use scanf. – Mohan Oct 19 '16 at 11:02