I have an array:
char arr[]="This is the string";
For instance, if I want to print only first 5 characters of that string, I have tried the following:
printf("%-5s",arr);
But it is printing whole string. Why?
You can use %.*s, it takes size of intended bytes to be printed and pointer to char as arguments when using with printf. For example,
// It prints This
printf("%.*s", 4, arr);
But it is printing whole string. Why?
You are using %-5s meaning the - left-justifies your text in that field.
En passant, the output cannot be achieved by using the accepted answer as simply as the code snippet, even if it may be seemed derisively.
int i;
char arr[]="This is the string";
for (i = 1; i < sizeof(arr); ++i) {
printf("%.*s\n", i, arr);
}
Output:
T
Th
Thi
This
This
This i
This is
This is
This is t
This is th
This is the
This is the
This is the s
This is the st
This is the str
This is the stri
This is the strin
This is the string
- is a printf formater for justification, not precision.
What you want is the . formater which is used for precision :
printf("%.5s", arr);
This will print the first 5 elements of arr.
If you want to learn more about printf formaters, take a look at this link.
for example substring extraction function (extracts substring to the buff)
char *strpart(char *str, char *buff, int start, int end)
{
int len = str != NULL ? strlen(str) : -1 ;
char *ptr = buff;
if (start > end || end > len - 1 || len == -1 || buff == NULL) return NULL;
for (int index = start; index <= end; index++)
{
*ptr++ = *(str + index);
}
*ptr = '\0';
return buff;
}
You can do it quite simply in a number of ways. With a loop, looping the number of times desired, picking off chars each time, you can walk a pointer down the string an temporary nul-terminate after the 5th character, or you can simply use strncpy to copy 5 chars to a buffer and print that. (that is probably the simplest), e.g.
#include <stdio.h>
#include <string.h>
int main (void)
{
char arr[]="This is the string",
buf[sizeof arr] = ""; /* note: nul-termination via initialization */
strncpy (buf, arr, 5);
printf ("'%s'\n", buf);
return 0;
}
Example Use/Output
$ ./bin/strncpy
'This '
Look things over and let me know if you have any questions.
Related
I am trying to reverse a string (character array) using the following code, but when I attempt to print the string, the value of null. This is a homework assignment, but I am trying to learn so any help would be appreciated.
void input_reverse_string(const char* inputStr, char* reverseStr)
{
int i = 0;
int length = 0;
for (; *(inputStr++) != '\0'; i++)
{
length++;
}
while (*inputStr)
{
*reverseStr = *inputStr;
inputStr++;
reverseStr++;
}
const char* chr_ptr = &inputStr[length - 1];
printf("I see a %s\n", *chr_ptr);
*reverseStr = '\0';
printf("%d", length);
/* return reverseStr; */
}
Several things are out of order:
That's a strange way of computing the length of a string. You are using an index variable that you don't need, and incrementing 3 things at the same time, it's unneeded to say the least.
After calculating the length, and incrementing the inputStr pointer up to its end, you don't reset the pointer, so it still points to the end of the string (actually, one after the end!).
Inside the while you are advancing both pointers (inputStr and reverseStr) in the same direction, which can't possibly be right if you want to reverse the string.
The correct way to do this would be:
Compute the length of the string. Either use strlen() or do it by hand, but you really only need to increment one variable to do this. You can avoid incrementing inputStr, just use a temporary pointer.
Start from inputStr + length and walk backwards. Either use a pointer and do -- or just index the string).
Here's a working example:
void reverse_string(const char* inputStr, char* reverseStr) {
unsigned len = 0;
int i;
while (inputStr[len])
len++;
for (i = len - 1; i >= 0; i--) {
reverseStr[len - i - 1] = inputStr[i];
}
reverseStr[len] = '\0';
}
int main(void) {
char a[6] = "hello";
char b[6];
reverse_string(a, b);
puts(b);
return 0;
}
Output:
olleh
I am trying to reverse a string. scanf is working well but when I use fixed string then it gives garbage value. So where is the fault ?
#include<stdio.h>
#include<string.h>
int main()
{
char s[50]="Hi I Love Programming";
char rev[strlen(s)];
int i,k;
k=strlen(s);
for(i=0; i<strlen(s); i++)
{
rev[k]=s[i];
k--;
}
printf("The reverse string is: %s\n", rev);
}
Your program has two issues:
1.
char rev[strlen(s)];
You forgot to add an element for the string-terminating null character '\0'.
Use:
char rev[strlen(s) + 1];
Furthermore you also forgot to append this character at the end of the reversed string.
Use:
size_t len = strlen(s);
rev[len] = '\0';
Note, my len is the k in your provided code. I use the identifier len because it is more obvious what the intention of that object is. You can use strlen(s) because the string has the same length, doesn´t matter if it is in proper or reversed direction.
2.
k=strlen(s);
for(i=0; i<strlen(s); i++)
{
rev[k]=s[i];
k--;
}
With rev[k] you accessing memory beyond the array rev, since index counting starts at 0, not 1. Thus, the behavior is undefined.
k needs to be strlen(s) - 1.
Three things to note:
The return value of strlen() is of type size_t, so an object of type size_t is appropriate to store the string length, not int.
It is more efficient to rather calculate the string length once, not at each condition test. Use a second object to store the string length and use this object in the condition of the for loop, like i < len2.
char s[50]="Hi I Love Programming"; can be simplified to char s[]="Hi I Love Programming"; - The compiler automatically detects the amount of elements needed to store the string + the terminating null character. This safes unnecessary memory space, but also ensures that the allocated space is sufficient to hold the string with the null character.
The code can also be simplified (Online example):
#include <stdio.h>
#include <string.h>
int main(void)
{
char s[] = "Hi I Love Programming";
size_t len = strlen(s);
char rev[len + 1];
size_t i,j;
for(i = 0, j = (len - 1); i < len; i++, j--)
{
rev[j] = s[i];
}
rev[len] = '\0';
printf("The reverse string is: %s\n", rev);
}
Output:
The reverse string is: pgnimmargorP evoL I iH
your program is hard to understand. Here you have something much simpler (if you want to reverse the string of course)
#include <stdio.h>
#include <string.h>
char *revstr(char *str)
{
char *start = str;
char *end;
if(str && *str)
{
end = str + strlen(str) - 1;
while(start < end)
{
char tmp = *end;
*end-- = *start;
*start++ = tmp;
}
}
return str;
}
int main()
{
char s[50]="Hi I Love Programming";
printf("%s", revstr(s));
}
https://godbolt.org/z/5KX3kP
Really need help!
This code should find a largest palindrome in a string. Which means if there are "abcdcba" and "cdc" in the same string, it should print "abcdcba" out since the length is longer. The function takes a string str and 2 points i and j and determines whether the string from i to j is a palindrome. If it is a palindrome, returns the length of the palindrome and if it is not, returns -1.
int palindromelength(char *str, int i, int j){
int *first = &i, *last = &j;
int len;
while (first < last){
if (toupper(*first) != toupper(*last))
return -1;
first++;
last--;
}
len = last - first;
return (len);
}
int main() {
int length, i, j;
char str;
scanf("%s", &str);
length = strlen(str);
printf("Length = %d\n", palindromelength(str, i, j));
//should print out largest palindrome.
return 0;
}
What you describe and what the function is supposed to do are inconsistent:
"The function takes a string str and 2 points i and j and determines whether the string from i to j is a palindrome. If it is a palindrome, returns the length of the palindrome and if it is not, returns -1"
Therefore the function should returen either j - i or -1
char str;
scanf("%s", &str);
This is not how you should declare then initialize a string. Use instead:
char str[512];
scanf ("%s", str);
Also note that you'll need to ask the user to input the length of that string, and you'll need to pass that length as argument in the "palindromelength" function
You access the (i+1)th entry of your string like this:
str[i]
but before, you need to check that i is strictly lower than the length of your string. And don't forget to initialize i and j in your main() function
Before starting to code, write an algorithm in pseudocode which can solve your problem and evaluate its complexity. In this case, the comlpexity of the most obvious algorithm would be O(n^2), where n is the length of the string. This most obvious solution would be to check every substring, but maybe there are better algorithms: en.wikipedia.org/wiki/Longest_palindromic_substring
You send your function a string and two indexes, then immediately take the address of two indexes and proceeded to increment/decrement the indexes without any relation or regard to your string. That will never get you anywhere.
While it is fine to try and do all comparisons with indexes as integers, it is probably a bit easier to approach finding palindromes operating on strings and characters instead. You use the indexes to set the start and end position within the string. The indexes by themselves are just numbers. Your first and last should not hold the address to the intergers, they should hold the address of the first and last characters of your search string.
Below is a quick example of using the indexes to locate the start and end characters for your search. Note: I use p (pointer) for first and ep (end pointer) for your last. Look over the logic and let me know if you have questions. The program takes 3 arguments, the string, start and end indexes within the string (0 based indexes):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXP 128
int get_palindrome (char *s, size_t i, size_t j);
int main (int argc, char **argv) {
if (argc < 4 ) {
fprintf (stderr, "error: insufficient input, usage: %s str i j\n", argv[0]);
return 1;
}
size_t i = atoi (argv[2]);
size_t j = atoi (argv[3]);
char palindrome[MAXP] = {0};
int len = get_palindrome (argv[1], i, j);
if (len > 0)
{
strncpy (palindrome, argv[1] + i, len);
printf ("\n palindrome: %s (%d chars)\n\n",
palindrome, len);
}
else
printf ("\n no palindrome for for given indexes.\n\n");
return 0;
}
int get_palindrome (char *s, size_t i, size_t j)
{
if (!s || *s == 0) return -1;
int len = 0;
char *p = s + i; /* set start/end pointers */
char *ep = s + j + 1;
char *sp = NULL;
int c = *ep; /* save char from string */
*ep = 0; /* null-terminate at end */
char *s2 = strdup (p); /* copy string to s2 */
*ep = c; /* replace original char */
p = s2; /* set s2 start/end ponters */
ep = s2 + j - i;
while (ep > p) /* check for palindrome */
{
if (toupper(*ep) != toupper(*p))
{
*ep = 0;
sp = NULL;
}
else if (!sp)
sp = p;
p++, ep--;
}
len = sp ? (int) strlen (sp) : -1; /* get length */
if (s2) free (s2); /* free copy of string */
return len; /* return len or -1 */
}
Output
$ ./bin/palindrome 1234abcdcba 4 10
palindrome: abcdcba (7 chars)
Note the use of size_t type instead of int for i & j. i & j are indexes and will not be negative for the purpose of this problem. Try to always choose your data type to best fit your data. It will help identify and prevent problems in your code.
Also note, you should make a copy of the string in the function (if you are concerned about preserving the original). Inserting null-terminating characters locating palindromes will alter the original string otherwise.
There is an easy to understand solution to find out longest palindrome in a string.
Key Concept: at the center of a palindrome, characters are always of the form
"....x y x...." or "......x x......"
step1: scan the string from start to end for the xyx or xx patterns and store the center indices in an auxiliary array.
step2: now around each center try to expand the string in both direction and store the lengths.
step3: return the max length.
This approach takes O(N2) order time.
The code I have is quite simple in one method I have this:
// This line has an Intellisense Error: Initialization with {...} expected for aggregate object
char str[] = GetBuffer(); // x 64 will give us 512 (sector sized buffer) ;
The GetBuffer metod is this:
char * GetBuffer(void)
{
int idx = 0;
int offset = 0;
char *buffer[512];
for(idx =0; idx < 64; idx ++)
{
// This line has an Itellisense Error: "Expected Expression"
buffer[offset + idx] = {"E","R","A","S","E","D"," ", " "};
offset += 8;
}
return *buffer;
}
Any ideas what's wrong with this?
All I am trying to do - is populate a buffer with 512 bytes which contain the following string repeated: "ERASED " This is ANSI C (not C++) and it has been so long since I coded in ANSI C - please help and be kind!
Using Visual Studio 2012
EDIT 1
Ok lots of things have been fixed thanks to you guys - but no full answer yet.
The str buffer holds 528 characters and not 512 and contains a lot of ERASED as expected but ends with
ýýýý««««««««îþîþ
Any ideas with this? And Oh boy I have a great deal of pure C reading to do - I have forgotten way too much!
You can't initialize an array with the return value from a function.
You could use a pointer instead of an array:
char *str = GetBuffer();
Or you could use strcpy() or a relative — but there are buffer overflow risks:
char str[512];
strcpy(str, GetBuffer());
Your GetBuffer() function also has a lot of problems.
char *GetBuffer(void)
{
int idx = 0;
int offset = 0;
char *buffer[512];
This should probably be char buffer[512];, but...
for(idx =0; idx < 64; idx ++)
{
// This line has an Itellisense Error: "Expected Expression"
buffer[offset + idx] = {"E","R","A","S","E","D"," ", " "};
You can't set arrays like this. And you needed double quotes because of the char *buffer[512] problem.
offset += 8;
}
return *buffer;
}
And you should not return a local variable — it is destroyed when the function returns so it can't be used afterwards.
You might write:
char *GetBuffer(void)
{
char *buffer = malloc(257);
if (buffer != 0)
{
int idx;
for (idx = 0; idx < 256; idx += 8)
strcpy(buffer+idx, "ERASED ");
}
return buffer;
}
There's a small layer of obfuscation going on with the hard-coded lengths and limits; they're correct, but the interconnections between the sizes are not obvious — and ideally, they should be:
strlen("ERASED ") == 8
256 = 32 * strlen("ERASED ")
257 = 32 * strlen("ERASED ") + 1 (the one is for the terminal null)
And then the calling code might be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *str = GetBuffer();
if (str != 0)
{
printf("<<%s>>\n", str);
free(str);
}
return(0);
}
there is problem with your buffer creation. you'd malloc such that it's not reclaimed by the function invoke routine. Second, you can't do assignment like the line you encountered a Itellisense error.
You can use this:
#include "stdlib.h"
char * GetBuffer(void)
{
int i = 0, idx = 0;
const char * cstr_init = "ERASED ";
char *buffer = (char*)malloc(512);
for (idx = 0; idx < 512; idx+=8) {
for (i = 0; i < 8; i++) {
buffer[idx+i] = cstr_init[i];
}
}
return buffer;
}
There are several things wrong here.
In C, a character array can be initialized with an initializer list or a string literal. You cannot use the return value from a function to initialize the array. So
char str[] = GetBuffer();
will not work.
Also, char* buffer [512] is an array of 512 pointers to char, i.e., an array of 512 strings. buffer [offset + idx] would be one pointer to char. It can hold only one string, but you are trying to assign eight strings to it: "E", "R", etc. If you mean those to be chars and not strings, use single quotes: 'E', etc. However, even that won't work unless you allocate memory to the pointer so that it can hold the string.
As written, the array of pointers is allocated on the stack, so it goes out of scope when the function terminates. return *buffer would return the first string in the array of strings, but that's a local variable, so you're returning the dereferenced value of a pointer that is no longer in scope.
I think a simpler way to accomplish your goal is this:
char str [512] = {'\0'};
for (int i = 0; i < 511; i += 7)
strcat (str + i, "ERASED ");
It's not very general, but it does what you want.
Edited to reflect Jonathan Leffler's comment that strcat (str, "ERASED "), which is what I originally had, is inefficient.
Given a char[] in c, I want to specify the length of output from that array starting with some position in printf, how can I conveniently do that?
For example, I want to print the portion from position 3 (indexed from 0) to position 8:
printf("%s", char_array+3..char_array+8)
I could make a temporary char[] with the desired length, but that is not very convenient. Or I could write a substring function
char* substring(const char* str, size_t begin, size_t len) {
if (str == 0 || strlen(str) == 0 || strlen(str) < begin || strlen(str) < (begin + len))
return 0;
return strndup(str + begin, len);
}
But if I call
printf("%s", substring(s, 3, 5));
I am afraid there will be memory leak.
What do you think about the best way to do this?
#include <stdio.h>
int printmid(const char *data, size_t start, int len) {
return printf("%.*s", len, data + start);
}
int main(void) {
char data[] = "Hello, World!";
printmid(data, 3, 6);
puts("");
return 0;
}
output
lo, Wo
int end = 8;
int bgn = 3;
int len = end - bgn + 1;
printf("%*.*s", len, len, char_array + bgn);
Note that len should be an int (and not, for example, the difference between two pointers - that's a ptrdiff_t or size_t). You can also omit the first length if you want - both can be specified, and they have specific meanings if the source string is shorter than the designated length (meaning there's a null byte in the string before char_array + end). Clearly, if the string in char_array is shorter than 4 bytes, then what follows the terminating null is probably garbage, and GIGO applies. If you're not certain the string is at least 8 bytes long, you might prefer to have the output left justified, in which case, insert a minus - before the first *; as written, the shorter string would be right justified.