I don't understand how words in the sentence is reversed, using the next code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main(void) {
char *sent=(char*)calloc(120, sizeof(char*));
puts("enter sentence:");
gets(sent);
size_t n= strlen(sent);
char arr[n+1];
char *p = arr;
char *q = sent + n;
while (q != sent){
while (q != sent && isblank(*(q-1))) *p++ = *--q;
char *r = q;
while (r != sent && !isblank(*(r-1))) --r;
memcpy(p, r, q-r);
p+= q - r;
q = r;
}
puts("original sentence:");
puts(sent);
puts("reversed sentence:");
puts(arr);
return 0;
}
May somebody explain me what pointers *q and *r mean, please?
both, q and r, point to the end.
While q serves to process spaces (to point to the end of the word)
r is used to detect the beginning of a word.
Resp. memcpy(p, r, q-r)
takes a word from r of length q-r and places it in p
Related
How can I merge character by character two strings of different sizes to the n-th element of A and m-th element of B. I can easily do it when they have the same size (assuming here n=m). But cannot figure out how to handle this exception.
My working zip code is as follows:
char * zip(char *A, char *B, int n)
{
char *C;
int i;
C = malloc((2*n) * sizeof *A);
for(i=0; i<n; i++) {
C[(2*i)]=A[i];
C[(2*i)+1]=B[i];
}
return C;
}
But instead to pass just int n, I would like to pass also int m where n is to merge the n first elements of A and the m first elements of B. Hence passing the following input to the new_zip(char *A, char *B, int n, int m). A="rslxyzkw"; B="eutingxyz";n=3; m=6, I would get "resulting".
like this:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *new_zip(char *A, char *B, int n, int m){
assert(A != NULL && B != NULL && n >= 0 && m >= 0);
char *C = malloc(n + m + 1);//+1 for NUL
if(!C){
perror("malloc:");
return NULL;
}
int i = 0;
while(n + m > 0){
if(n > 0 && *A){
C[i++] = *A++;
--n;
}
if(m > 0 && *B){
C[i++] = *B++;
--m;
}
}
C[i] = 0;
return C;
}
int main (void){
char *result = new_zip("rslxyzkw", "eutingxyz", 3, 6);
printf("'%s'\n", result);
free(result);
return 0;
}
Instead of looping until a certain length you can loop until you run out of characters. C strings have a null character at the end so after you allocate the memory you can loop as long as both aren't a null character. All you need to do then is only add non-null characters to your output string.
#include <stdio.h> /* printf */
#include <stdlib.h> /* malloc, free */
#include <string.h> /* strlen */
char *zip(char *a, char *b)
{
char *c = malloc( (strlen(a)+strlen(b)+1) * sizeof(char) ), *p = c;
if(c)
{
while(*a || *b) /* while either string has characters */
{
if(*a) *p++ = *a++; /* add a character from a if non-null */
if(*b) *p++ = *b++; /* add a character from b if non-null */
}
*p='\0'; /* finish the string with a null character */
}
return c;
}
int main()
{
char *a = "This is a string";
char *b = "This is another longer string";
char *c = zip(a,b);
if(c)
{
printf("zip(%s,%s) = %s\n",a,b,c);
free(c);
}
return 0;
}
The following could work.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* zip(char* A, char* B) {
char *C;
int k = 0;
C = (char*)malloc(strlen(A)+strlen(B)+1);
while (*A != '\0' || *B != '\0') {
if (*A != '\0') {
C[k++] = *A;
++A;
}
if (*B != '\0') {
C[k++] = *B;
++B;
}
}
C[k] = '\0';
return C;
}
int main() {
char *A = "123456", *B = "abcd", *C;
C = zip(A, B);
puts(C);
return 0;
}
The below code would work in the following way:
First do alternate merging from strings s1 and s2 based on the minimum value of m and n.
The second part would take care of appending the remaining elements either from s1 or from s2.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* merge (char *s1, char *s2, int m, int n)
{
char *s = (char *) malloc(m + n + 1);
int min = (m < n)? m: n;
int i = 0, j = 0, k = 0;
int count = 0;
/* Alternate merge from s1 and s2 to s*/
while (count < 2 * min) {
if (count % 2 == 0) {
s[k++] = s1[i++];
} else {
s[k++] = s2[j++];
}
count++;
}
/* Append the remaining elements from s1 or s2 to s*/
if (m > min) {
for (count = 0; count < m - min; count++) {
s[k++] = s1[i++];
}
} else if (n > min) {
for (count = 0; count < n - min; count++) {
s[k++] = s2[j++];
}
}
s[k++] = '\0';
return s;
}
int main()
{
char *s1 = "rslxyzkw";
char *s2 = "eutingxyz";
char *s = merge(s1, s2, 3, 6);
printf ("%s\n", s);
}
I tried following two ways to reverse a string in C using char pointers:
Option 1:
void stringreverse(char *s){
int n = stringlength(s) - 2;
while(*s != '\0'){
char c = *s;
*s = *(s+n);
*(s+n) = c;
s++;n--;
}
}
Option 2:
void stringreverse(char *s){
char *p = s + stringlength(s) - 2;
while(*s != '\0'){
char c = *s;
*s = *p;
*p = c;
s++,p--;
}
}
None of the above works. Hints on why?
The problem is that your code reverses the string and then reverse it again, because your loop goes from 0 to len (when *s==\0), it should stop at (len-1)/2
You should try this :
void stringreverse(char* s){
int len = strlen(s)-1;
int i;
for(i=0;i<len/2;i++){
char tmp = s[i];
s[i] = s[len-i];
s[len-i]=tmp;
}
}
To reverse the string you should swap the chars between the beginning and the end of the string until they meet in the middle, the way you did will reverse it and then reverse it again to the original string. Also there is strlen in standard C. Anyway using your definition of stringlength, it should be:
void stringreverse(char *s){
int n = stringlength(s) - 2;
int i;
while(i = 0; i < n / 2; i++) {
char c = s[i];
s[i] = s[n-i];
s[n-i] = c;
}
}
complete working sample using pointers:
#include <stdio.h>
void reverse(char *p){
char c;
char* q = p;
while (*q) q++;
q--; // point to the end
while (p < q){
c = *p;
*p++ = *q;
*q-- = c;
}
}
int main(){
char s[] = "DCBA";
reverse(s);
printf("%s\n", s); // ABCD
}
p: points to start of string.
q: points to the end of string.
then swap their contents.
simple and easy.
I've got a problem with some chars and i can't figure out how to solve it. It is given a sequence of strings and another string. And I have to count the number of appearances of the string in the sequence of strings. I did the program below but it doesn't work.
main.cpp
#include "tipulbool.h"
char sir1[25], sir2;
int n, i, k;
int main (){
cin>>n;
for(i = 1; i <= n; i++)
cin>>sir1[i];
cin>>sir2;
for(i = 1; i <= n; i++)
k += secventa(sir1[i], sir2);
cout<<k;
return 0;
}
tipulbool.h
#include <iostream>
#include <string.h>
using namespace std;
int secventa (char sir1[], char sir2);
tipulbool.cpp
#include "tipulbool.h"
int secventa (char sir1[], char sir2){
int contor;
char *p;
p = strstr(sir1[], sir2);
if(p)
contor++;
while(p){
p = strstr(p + 1, sir2);
if(p)
contor++;
}
return contor;
}
try this:
int countOfChars(char sir1[], char sir2)
{
int count = 0;
char* p = sir1;
while (*p)
{
if (p == sir2)
{
++count;
}
++p;
}
return count;
}
this code:
#include "tipulbool.h"
int secventa (char sir1[], char sir2){
int contor;
char *p;
p = strstr(sir1[], sir2);
if(p)
contor++;
while(p){
p = strstr(p + 1, sir2);
if(p)
contor++;
}
return contor;
}
gets lost in it self.
suggest:
#include "tipulbool.h"
int secventa (char sir1[], char sir2){
int contor = 0;
char *p = str1;
while(NULL != (p = strchr(p, str2) ) )
{
contor++;
p++;
}
return contor;
}
However, note that
1) I used `strchr()` rather than `strstr()`
because `str2` is a single char, not a string
2) I removed the repetitive code
3) `str1` MUST be a NULL terminated string, which in the posted code is not the case.
regarding this code:
#include "tipulbool.h"
char sir1[25], sir2;
int n, i, k;
int main (){
cin>>n;
for(i = 1; i <= n; i++)
cin>>sir1[i];
cin>>sir2;
for(i = 1; i <= n; i++)
k += secventa(sir1[i], sir2);
cout<<k;
return 0;
}
in C++, an array offset starts with 0 and continues to (length of array -1)
variables that are only used in a single function should (in general) be defined as local/auto variables within that function.
suggest using code similar to the following:
#include "tipulbool.h"
int main ( void )
{
char sir2;
int n; // will contain number of char in str1
int i; // loop counter
int k = 0; // will contain number of occurrence of str2 in str1
// get count of chars in first string
cin >> n;
// allocate room for first string (using C string)
// +1 to allow for NUL terminator byte
char *sir1 = new char[n+1];
// initialize first string
memset( sir1, 0x00, n+1 );
// input first string
for(i = 0; i < n; i++)
cin >> sir1[i];
// input target char
cin >> sir2;
// get count of occurances of str2 in str1
k = secventa(sir1, sir2);
cout << k << endl;
delete [] str1;
return 0;
}
Since this is C++, you might want to look at vector or string
to simplify the written part of the code even further
I'm doing the following exercise:
Example:
"abcde"
Output:
a
b
c
d
e
Here the template code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* diag(char* str, char* r)
{
/* Write code */
r[0] = '\0';
return r;
}
int main (void)
{
char str [] = "abcde";
int n = strlen(str);
char* r = malloc(n * n + 1);
r = diag(str, r);
puts(r);
free(r);
return 0;
}
Someone could guide me a little the function must be recursive, not know how to start
The trick with recursion is to consider what condition you need to make the necessary changes. You've already gotten everything set up, now think about how you would increment the array so that you can assign the letters to the diagonal positions (there's a pattern). There also needs to be a terminating case.
You can use this:
printDiagonally(char* s, int low, int high){
if(low < high){
int i;
for(i=0 ; i<low; i++)
printf(" ");
printf("%c\n", *s);
printDiagonally(++s, low+1, high);
}
}
My version of strncat is copying one too many chars into the destination and I cannot figure out why.
#include <stdio.h>
#define MAX_CHARS 20
void nconcatenate(char *start, char *end, int n)
{
if(sizeof start + n > MAX_CHARS)
return;
while(*start++);
start--; /* now points to the final char of start, the \0 */
int i;
for(i = 0; (*start++ = *end++) && i < n; i++);
*start = '\0';
}
int main()
{
char start[MAX_CHARS] = "str";
char *end = "ingy!";
nconcatenate(start, end, 3);
printf("start = %s\n", start);
return 0;
}
Using 3 as 'n' outputs
stringy
which is one too many chars.
Maybe because in the condition
(*start++ = *end++) && i < n
first it does (*start++ = *end++) and after that, it checks i < n.
I haven't tested it, but check it out and see.