Reputation: 3
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
'
Upvotes: 0
Views: 75
Reputation: 1901
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);
}
Upvotes: 0
Reputation: 11
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);
}
Upvotes: 1
Reputation: 34585
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.
Upvotes: 0
Reputation: 24587
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 bys
. Reading stops after anEOF
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.
Upvotes: 1
Reputation: 224437
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.
Upvotes: 0